Obtain the current URL, controller, and action in ASP. net mvc.

Source: Internet
Author: User
Tags httpcontext

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

Related Article

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.