Home  >  Article  >  Backend Development  >  Get the current URL, controller, and action image and text instances in ASP.NET MVC

Get the current URL, controller, and action image and text instances in ASP.NET MVC

PHPz
PHPzOriginal
2017-04-23 16:09:582592browse

This article introduces how to get the current URL, controller, and action in ASP.NET MVC. Please see the picture below

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!

Statement:
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