ASP. NET implements the method for obtaining the current URL, controller, and action in MVC, mvccontroller

Source: Internet
Author: User

ASP. NET implements the method for obtaining the current URL, controller, and action in MVC, mvccontroller

This example describes how to obtain the current URL, controller, and action in ASP. net mvc. We will share this with you for your reference. The details are as follows:

URL retrieval is simple. ASP. NET is common:

[1] Get the complete url

(Protocol name + domain name + virtual directory name + file name + parameter)

string url=Request.Url.ToString();

[2] obtain the virtual directory name + Page name + parameter:

string url=Request.RawUrl;

Or

string url=Request.Url.PathAndQuery;

[3] Get

Virtual directory name + Page name:

string url=HttpContext.Current.Request.Url.AbsolutePath;

Or:

string url= HttpContext.Current.Request.Path;

[4] retrieve domain names:

string url=HttpContext.Current.Request.Url.Host;

[5] GET parameters:

string url= HttpContext.Current.Request.Url.Query;

[6] obtain the Port:

Request.Url.Port

2. Obtain the current controller and action

RouteData.Route.GetRouteData(this.HttpContext).Values["controller"]RouteData.Route.GetRouteData(this.HttpContext).Values["action"]

Or:

RouteData.Values["controller"]RouteData.Values["action"]

If you can use:

ViewContext.RouteData.Route.GetRouteData(this.Context).Values["controller"]ViewContext.RouteData.Route.GetRouteData(this.Context).Values["action"]

Or:

ViewContext.RouteData.Values["controller"]ViewContext.RouteData.Values["action"]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.