Home > Backend Development > C#.Net Tutorial > Detailed explanation of the ASP.NET method of obtaining controller, URL and action in MVC

Detailed explanation of the ASP.NET method of obtaining controller, URL and action in MVC

Y2J
Release: 2017-05-05 11:55:34
Original
2779 people have browsed it

This article mainly introduces the method of obtaining the current URL, controller and action in ASP.NET MVC. It analyzes the specific implementation skills of asp.net MVC to obtain the current URL, controller and action in the form of examples. Friends who need it can Refer to the following

The example of this article describes the method of obtaining the current URL, controller and action in ASP.NET implementation of MVC. Share it with everyone for your reference, the details are as follows:

Obtaining the URL is very simple, common for ASP.NET:

[1] Get the complete url

(Protocol name + domain name + virtual directory name + file name + parameters)

string url=Request.Url.ToString();
Copy after login

【2】Get the virtual directory name + page name + parameters:

string url=Request.RawUrl;
Copy after login

or

string url=Request.Url.PathAndQuery;
Copy after login

【3】Get the

virtual directory name + page name:

string url=HttpContext.Current.Request.Url.AbsolutePath;
Copy after login

or:

string url= HttpContext.Current.Request.Path;
Copy after login

【4】Get the domain name:

string url=HttpContext.Current.Request.Url.Host;
Copy after login

【5】 Obtain parameters:

string url= HttpContext.Current.Request.Url.Query;
Copy after login

[6] Obtain port:

Request.Url.Port
Copy after login

2. Obtain current controller and action

RouteData.Route.GetRouteData(this.HttpContext).Values["controller"]
RouteData.Route.GetRouteData(this.HttpContext).Values["action"]
Copy after login

or:

RouteData.Values["controller"]
RouteData.Values["action"]
Copy after login

If you can use it in view:

ViewContext.RouteData.Route.GetRouteData(this.Context).Values["controller"]
ViewContext.RouteData.Route.GetRouteData(this.Context).Values["action"]
Copy after login

or:

ViewContext.RouteData.Values["controller"]
ViewContext.RouteData.Values["action"]
Copy after login

[Related recommendations]

1. ASP free video Tutorial

2. ASP Tutorial

3. Li Yanhui ASP Basic Video Tutorial

The above is the detailed content of Detailed explanation of the ASP.NET method of obtaining controller, URL and action in MVC. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template