ASP.NET MVC和ASP.NET Core MVC中获取当前URL/Controller/Action
2021-02-19 16:18
标签:etc esc any and ted onclick author tail tle
ASP.NET MVC 一、获取URL(ASP.NET通用): 【1】获取完整url(协议名+域名+虚拟目录名+文件名+参数) 【2】获取虚拟目录名+页面名+参数: 【3】获取虚拟目录名+页面名: 【4】获取域名: 【5】获取参数: 【6】获取端口: 二、获取当前Controller/Action: 在视图中 很显然上面只能获取当前View的,如果是嵌套的View上面获得的最终的子View的,如果要获取父View的,如下 参考文献: asp.net MVC中获取当前URL/Controller/Action ASP.NET Core MVC 一、获取URL: 定义一个静态类HttpRequestExtensions,写一个HttpRequest的扩展方法GetAbsoluteUri,需要引入 Microsoft.AspNetCore.Http.Abstractions包: 其中关于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: 结果如下所示: 参考文献: asp.net core 获取当前请求的url How to get current url in view in asp.net core 1.0 二、获取当前Controller/Action 在Controller中: 在视图中: 在Filter拦截器中: 我们这里就以IAuthorizationFilter拦截器举例获取当前Controller/Action,其它Filter拦截器(IActionFilter、IResourceFilter、IExceptionFilter等)以此类推: 在HomeController上注册我们定义的IAuthorizationFilter拦截器AuthorizationFilterAttribute即可: 下载ASP.NET Core MVC中Filter拦截器获取当前Controller/Action的示例代码 ASP.NET MVC和ASP.NET Core MVC中获取当前URL/Controller/Action 标签:etc esc any and ted onclick author tail tle 原文地址:https://www.cnblogs.com/lonelyxmas/p/12928352.html
string url=Request.Url.ToString();
string url=Request.RawUrl;(或 string url=Request.Url.PathAndQuery;)
string url=HttpContext.Current.Request.Url.AbsolutePath;(或 string url= HttpContext.Current.Request.Path;)
string url=HttpContext.Current.Request.Url.Host;
string url= HttpContext.Current.Request.Url.Query;
Request.Url.Port
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"]
ControllerContext.ParentActionViewContext.RouteData.Values["controller"]
ControllerContext.ParentActionViewContext.RouteData.Values["action"]
在视图中
@ViewContext.ParentActionViewContext.RouteData.Values["controller"]
@CViewContext.ParentActionViewContext.RouteData.Values["action"]
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();
}
}
}
public IActionResult Index()
{
string url = HttpContext.Request.GetAbsoluteUri();
this.ViewData["url"] = url;
return View();
}
ControllerContext.ActionDescriptor.ControllerName
ControllerContext.ActionDescriptor.ActionName
@ViewContext.RouteData.Values["Controller"]
@ViewContext.RouteData.Values["Action"]
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的类名、命名空间和程序集名称
}
}
}
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 MVC和ASP.NET Core MVC中获取当前URL/Controller/Action
文章链接:http://soscw.com/index.php/essay/57598.html