gpt4 book ai didi

asp.net-core - 我可以从 AspNetCore FilterContext 获取 RouteTemplate 吗?

转载 作者:行者123 更新时间:2023-12-03 23:55:53 25 4
gpt4 key购买 nike

在 AspNetCore 中,给定一个 FilterContext,我正在寻找一个路由模板,例如{controller}/{action}/{id?}
在 Microsoft.AspNet.WebApi 中,我可以从以下位置获取路由模板:HttpControllerContext.RouteData.Route.RouteTemplate
在 System.Web.Mvc 我可以从:ControllerContext.RouteData.Route as RouteBase
在 AspNetCore 中有:FilterContext.ActionDescriptor.AttributeRouteInfo.Template
但是,并非所有路由都是属性路由。

如果属性不可用,则根据检查,可以从以下位置组装默认路由和/或映射路由:FilterContext.RouteData.Routers.OfType<Microsoft.AspNetCore.Routing.RouteBase>().First()但我正在寻找一个有记录的或更好的方法。

最佳答案

更新(2021 年 1 月 24 日)
检索 RoutePattern 的方法要简单得多。直接通过HttpContext .

    FilterContext filterContext;
var endpoint = filterContext.HttpContext.GetEndpoint() as RouteEndpoint;
var template = endpoint?.RoutePattern?.RawText;
if (template is null)
throw new Exception("No route template found, that's absurd");
Console.WriteLine(template);
GetEndpoint()EndpointHttpContextExtensions 中提供的扩展方法类内 Microsoft.AspNetCore.Http命名空间
旧答案(工作太多)
ASP.NET Core 应用程序(至少对于 3.1)的所有路由构建器都通过 IEndpointRouteBuilder 公开和注册,但是很遗憾,这个没有注册到DI容器中,所以你不能直接获取它。我看到这个接口(interface)暴露的唯一地方,是在中间件中。
因此,您可以使用其中一个中间件构建集合或字典,然后将其用于您的目的。
例如
程序.cs
扩展类来构建你的端点集合/字典
    internal static class IEndpointRouteBuilderExtensions
{
internal static void BuildMap(this IEndpointRouteBuilder endpoints)
{
foreach (var item in endpoints.DataSources)
foreach (RouteEndpoint endpoint in item.Endpoints)
{
/* This is needed for controllers with overloaded actions
* Use the RoutePattern.Parameters here
* to generate a unique display name for the route
* instead of this list hack
*/

if (Program.RouteTemplateMap.TryGetValue(endpoint.DisplayName, out var overloadedRoutes))
overloadedRoutes.Add(endpoint.RoutePattern.RawText);
else
Program.RouteTemplateMap.Add(endpoint.DisplayName, new List<string>() { endpoint.RoutePattern.RawText });
}
}
}

public class Program
{
internal static readonly Dictionary<string, List<string>> RouteTemplateMap = new Dictionary<string, List<string>>();
/* Rest of things */
}
启动.cs
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
/* all other middlewares */
app.UseEndpoints(endpoints =>
{

endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");

//Use this at the last middlware exposing IEndpointRouteBuilder so that all the routes are built by this point
endpoints.BuildMap();
});
}
然后您可以使用该字典或集合,从 FilterContext 中检索路由模板。
    FilterContext filterContext;
Program.RouteTemplateMap.TryGetValue(filterContext.ActionDescriptor.DisplayName, out var template);
if (template is null)
throw new Exception("No route template found, that's absurd");

/* Use the ActionDescriptor.Parameters here
* to figure out which overloaded action was called exactly */
Console.WriteLine(string.Join('\n', template));
为了解决重载 Action 的情况,一个字符串列表用于路由模板(而不仅仅是字典中的一个字符串)
您可以使用 ActionDescriptor.Parameters结合 RoutePattern.Parameters为该路线生成唯一的显示名称。

关于asp.net-core - 我可以从 AspNetCore FilterContext 获取 RouteTemplate 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47576702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com