This article describes how to obtain the current URL, controller, and action in ASP. net mvc. For more information about asp.net mvc, see.
I. Getting URLs is simple. ASP. NET is common:
[1] obtain 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] obtain the 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"]