ASP.NET MVC和ASP.NET Core MVC中获取当前URL/Controller/Action (转载)

ASP.NET MVC


一、获取URL(ASP.NET通用):

【1】获取完整url(协议名+域名+虚拟目录名+文件名+参数)

string url=Request.Url.ToString();

【2】获取虚拟目录名+页面名+参数:

string url=Request.RawUrl;(或 string url=Request.Url.PathAndQuery;)

【3】获取虚拟目录名+页面名:

string url=HttpContext.Current.Request.Url.AbsolutePath;(或 string url= HttpContext.Current.Request.Path;)

【4】获取域名:

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

【5】获取参数:

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

【6】获取端口:

Request.Url.Port

二、获取当前Controller/Action:

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

RouteData.Values["controller"]
RouteData.Values["action"]

在视图中

@ViewContext.RouteData.Route.GetRouteData(this.Context).Values["controller"]
@ViewContext.RouteData.Route.GetRouteData(this.Context).Values["action"]

@ViewContext.RouteData.Values["controller"]
@ViewContext.RouteData.Values["action"]

很显然上面只能获取当前View的,如果是嵌套的View上面获得的最终的子View的,如果要获取父View的,如下

ControllerContext.ParentActionViewContext.RouteData.Values["controller"]
ControllerContext.ParentActionViewContext.RouteData.Values["action"]
在视图中
@ViewContext.ParentActionViewContext.RouteData.Values["controller"]
@CViewContext.ParentActionViewContext.RouteData.Values["action"]

参考文献:

asp.net MVC中获取当前URL/Controller/Action

ASP.NET Core MVC


一、获取URL:

定义一个静态类HttpRequestExtensions,写一个HttpRequest的扩展方法GetAbsoluteUri,需要引入 Microsoft.AspNetCore.Http.Abstractions包:

using Microsoft.AspNetCore.Http;
using System.Text; namespace AspNetCoreUrl
{
public static class HttpRequestExtensions
{
public static string GetAbsoluteUri(this HttpRequest request)
{
return new StringBuilder()
.Append(request.Scheme)
.Append("://")
.Append(request.Host)
.Append(request.PathBase)
.Append(request.Path)
.Append(request.QueryString)
.ToString();
}
}
}

其中关于HttpRequest.Path和HttpRequest.PathBase有什么不同,可以查看:

What's the difference between HttpRequest.Path and HttpRequest.PathBase in ASP.NET Core?

然后我们就可以在使用HttpRequest时,调用该扩展方法来获取当前URL,例如下面我们在ASP.NET Core MVC的Controller中获取了当前URL:

public IActionResult Index()
{
string url = HttpContext.Request.GetAbsoluteUri();
this.ViewData["url"] = url; return View();
}

结果如下所示:

ASP.NET MVC和ASP.NET Core MVC中获取当前URL/Controller/Action (转载)

参考文献:

asp.net core 获取当前请求的url

How to get current url in view in asp.net core 1.0

二、获取当前Controller/Action

在Controller中:

ControllerContext.ActionDescriptor.ControllerName
ControllerContext.ActionDescriptor.ActionName

在视图中:

@ViewContext.RouteData.Values["Controller"]
@ViewContext.RouteData.Values["Action"]

在Filter拦截器中:

我们这里就以IAuthorizationFilter拦截器举例获取当前Controller/Action,其它Filter拦截器(IActionFilter、IResourceFilter、IExceptionFilter等)以此类推:

using Microsoft.AspNetCore.Mvc.Filters;
using System; namespace AspNetCoreFilterContext.Filters
{
public class AuthorizationFilterAttribute : Attribute, IAuthorizationFilter
{
public void OnAuthorization(AuthorizationFilterContext context)
{
string controllerName = context.RouteData.Values["Controller"].ToString();//通过ActionContext类的RouteData属性获取Controller的名称:Home
string actionName = context.RouteData.Values["Action"].ToString();//通过ActionContext类的RouteData属性获取Action的名称:Index string fullActionName = context.ActionDescriptor.DisplayName;//通过ActionContext类的ActionDescriptor属性,也可以获取Action的名称
//不过这样获取到的是Action的完全限定名:"AspNetCoreFilterContext.Controllers.HomeController.Index (AspNetCoreFilterContext)",可以看到其中还包含Controller的类名、命名空间和程序集名称
}
}
}

在HomeController上注册我们定义的IAuthorizationFilter拦截器AuthorizationFilterAttribute即可:

using Microsoft.AspNetCore.Mvc;
using AspNetCoreFilterContext.Filters; namespace AspNetCoreFilterContext.Controllers
{
[AuthorizationFilter]
public class HomeController : Controller
{
public IActionResult Index()
{
string controllerName = ControllerContext.ActionDescriptor.ControllerName;
string actionName = ControllerContext.ActionDescriptor.ActionName; return View();
}
}
}

下载ASP.NET Core MVC中Filter拦截器获取当前Controller/Action的示例代码

上一篇:fibonacci数列(五种)


下一篇:spring MVC中获取request和response: