- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 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
命名空间
IEndpointRouteBuilder
公开和注册,但是很遗憾,这个没有注册到DI容器中,所以你不能直接获取它。我看到这个接口(interface)暴露的唯一地方,是在中间件中。
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/
我有 ASP.NET Core Web 应用程序,我在其中使用 swagger 使用以下内容: public void ConfigureServices(IServiceCollection ser
AspNetCore.Mvc 和 AspNetCore.Mvc.Core NuGet 包之间有什么区别? Mvc.Core 只是简单的东西,而 Mvc 是一个包罗万象的包吗?这就是我从描述中猜测的he
我有一个包含多个项目的解决方案。我想创建一个项目的Docker镜像,因此我已经通过Docker支持添加了一个Dockerfile。我添加了Dockerfile的项目已在同一级别上依赖于其他项目。当我尝
我正在尝试使用 Ajax 将类列表返回到我的 View 这是我的ajax $(document).ready(function () { debugger; $.
我已经使用过几次这个包Microsoft.AspNetCore.TestHost在我的集成测试中托管 Asp.Net Core Web API 应用程序。 使用 Asp.Net Core 2.1 包
我正在尝试使用 Ajax 将类列表返回到我的 View 这是我的ajax $(document).ready(function () { debugger; $.
SignalR 客户端有两个 nuget 包: Microsoft.AspNetCore.SignalR.Client和 Microsoft.AspNetCore.SignalR.Client.Cor
这个错误的可能原因是什么: InvalidOperationException: No service for type 'Microsoft.AspNetCore.Identity.UserMana
使用 IFormFile 时,我在运行时收到此错误: Could not load type 'Microsoft.AspNetCore.Http.Internal.FormFile' from as
我正在尝试将我的 API 项目从 .net core 2.2 升级到 .net core 3.1。我在尝试进行 API 调用时遇到此异常。 "Message":"Could not load type
我在 netcoreapp3.0 Web 应用程序中使用 netstandard2.1 库。在 Startup 中添加我的服务时,出现以下错误: 'Could not load type 'Micro
我遇到以下异常: Cannot resolve parameter 'Microsoft.Extensions.Logging.ILogger logger' "At the moment (9/28
我们有使用 Swagger 的 .net core 2.1 mvc webapi 项目。 我们使用以下软件包: swashbuckle 的配置方式如下: services.AddMvcCo
我正在使用图书馆 "FluentValidation.AspNetCore": "6.4.0-beta3"在 .netcore WebApi在一个项目中。您可以在下面看到项目结构。如果我放置 Curr
怎么改Content root path在应用程序 .Net Core 3.0 ASP Blazor 上启动? 现在应用程序以输出开始 info: Microsoft.AspNetCore.DataP
我正在将 Microsoft.AspNetCore.Identity 添加到一个项目中,我得到了 InvalidOperationException: Unable to resolve servic
我有一个 Asp.Net Core 2 Mvc 项目。我目前正在尝试将数据访问分离到一个单独的项目中;但是,一旦我添加对数据访问库的引用,我就会遇到版本冲突: error NU1107: Versio
我理解的三大人生: 单例 有范围 短暂的 但我似乎找不到说明默认生命周期是多少(如果未明确定义)的文档。 最佳答案 默认情况下,注册类型的生命周期是 transient 的,即每次注入(inject)
我有一个正在泄漏线程的 ASP.NET Core 2 应用程序。我如何确定线程永不消亡的原因? 如果我在生产环境中运行该应用程序 10 分钟,IIS 开始吐出 502.3 Bad Gateway 错误
当尝试从新的DotNetCore类库导入AspNetCore MVC站点(WebApplication1)以便运行某些测试时,出现以下错误。 任何人都可以对编译器的工作原理以及为什么使用runtime
我是一名优秀的程序员,十分优秀!