ASP. NET Implementation of MVC Obtaining Current URL controller and action

  • 2021-09-11 19:57:53
  • OfStack

This paper describes the method of obtaining current URL, controller and action in ASP. NET implementation of MVC. Share it for your reference, as follows:

URL is easy to get, and ASP. NET is generic:

'1' Get full url

(Protocol Name + Domain Name + Virtual Directory Name + File Name + Parameters)


string url=Request.Url.ToString();

"2" Get virtual directory name + page name + parameters:


string url=Request.RawUrl;

Or


string url=Request.Url.PathAndQuery;

"3" Acquisition

Virtual directory name + page name:


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

Or:


string url= HttpContext.Current.Request.Path;

"4" Obtain domain name:


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

'5' Gets parameters:


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

'6' Acquisition port:


Request.Url.Port

2. Acquisition of 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:


string url=Request.RawUrl;

0

Or:


string url=Request.RawUrl;

1

More readers interested in asp.net can check the topics of this site: "Summary of asp.net Optimization Skills", "Summary of asp. net String Operation Skills", "Summary of XML Operation Skills", "Summary of asp. net File Operation Skills", "Summary of asp. net ajax Skills" and "Summary of asp. net Cache Operation Skills".

I hope this paper is helpful to everyone's asp. net programming.


Related articles: