Home > Article > Backend Development > Detailed explanation of the ASP.NET method of obtaining controller, URL and action in MVC
This article mainly introduces the method of obtaining the current URL, controller and action in ASP.NET MVC. It analyzes the specific implementation skills of asp.net MVC to obtain the current URL, controller and action in the form of examples. Friends who need it can Refer to the following
The example of this article describes the method of obtaining the current URL, controller and action in ASP.NET implementation of MVC. Share it with everyone for your reference, the details are as follows:
Obtaining the URL is very simple, common for ASP.NET:
[1] Get the complete url
(Protocol name + domain name + virtual directory name + file name + parameters)
string url=Request.Url.ToString();
【2】Get the virtual directory name + page name + parameters:
string url=Request.RawUrl;
or
string url=Request.Url.PathAndQuery;
【3】Get the
virtual directory name + page name:
string url=HttpContext.Current.Request.Url.AbsolutePath;
or:
string url= HttpContext.Current.Request.Path;
【4】Get the domain name:
string url=HttpContext.Current.Request.Url.Host;
【5】 Obtain parameters:
string url= HttpContext.Current.Request.Url.Query;
[6] Obtain port:
Request.Url.Port
2. Obtain 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 it in view:
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 recommendations]
2. ASP Tutorial
3. Li Yanhui ASP Basic Video Tutorial
The above is the detailed content of Detailed explanation of the ASP.NET method of obtaining controller, URL and action in MVC. For more information, please follow other related articles on the PHP Chinese website!