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"]