1. Obtaining the URL is very simple. ASP.NET general:
[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 domain name:
string url=HttpContext.Current.Request.Url.Host;
【5】Get parameters:
string url= HttpContext.Current.Request.Url.Query;
【6】Get port:
Request.Url.Port
2. Obtaining the current controller and action
RouteData.Route.GetRouteData(this.HttpContext).Values["controller"]
RouteData. Route.GetRouteData(this.HttpContext).Values["action"]
or
MVC master page RouteData.Values["controller"]
MVC master page RouteData.Values ["action"]
If you can use it in the 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"]
The above is the detailed content of Get the current URL, controller, and action image and text instances in ASP.NET MVC. For more information, please follow other related articles on the PHP Chinese website!