How to get the current URL, controller, action__.net in asp.net MVC

Source: Internet
Author: User
first, the URL of the acquisition is very simple, ASP. NET General:
"1" Get full URL (protocol name + domain name + virtual directory name + filename + parameter)

String url=request.url.tostring ();
"2" gets 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" Get the domain name:
String Url=httpcontext.current.request.url.host;

"5" Get the parameters:
String Url= HttpContext.Current.Request.Url.Query;

"6" Get port:
Request.Url.Port

Second, the current Controller, action of the acquisition
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 this 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"]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.