在ASP.NET Core 2中创建自定义路由属性

编程入门 行业动态 更新时间:2024-10-26 01:32:56
本文介绍了在ASP.NET Core 2中创建自定义路由属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我们有一堆端点,我们希望它们对每个端点执行完全相同的操作: 将它们注册为路由,并验证用户是否有权访问它们.非常精打细算,我们的问题可以归结为我们遇到这样的事情:

We have a bunch of endpoints where we'd like to do the exact same thing for each of them: Register them as a route and verify that the user has access to them. Very condensed our issue can be condensed to us having something like this:

[HttpGet, Route(EntityId.First)] [HttpGet, Route(EntityId.Second)] [VerifyAccessFilter(EntityId.First, EntityId.Second)] public async Task<IActionResult> Endpoint() { return Ok(); }

但更希望是这样的:

[RouteAndVerify(EntityId.First, EntityId.Second)] public async Task<IActionResult> Endpoint() { return Ok(); }

您可以说这很简单,但是我希望这个意图能传达出来. 困难的部分似乎是在不使用默认Route-attribute的情况下注册路由.

As you can tell this is very simplified, but I hope the intent gets across. The hard part seems to be registering the route without using the default Route-attribute.

推荐答案

您可以使用自定义的 IActionModelConvention 实现.官方文档解释了动作模型约定的概念:在ASP.NET Core-约定中使用应用程序模型.简而言之,通过实现IActionModelConvention,您可以更改应用程序模型,并在运行时为操作添加过滤器,路由等.

You can achieve this with a custom IActionModelConvention implementation. The official documentation explains the concept of an action model convention: Work with the application model in ASP.NET Core - Conventions. In a nutshell, by implementing IActionModelConvention, you can make changes to the application model and add filters, routes, etc to an action at runtime.

最好通过以下示例实现对此进行解释.当您想将现有的MVC过滤器与配置动作路由的功能结合在一起时,下面的实现同时实现了IResourceFilter(可以是您使用的任何过滤器类型)和IActionModelConvention:

This is best explained with a sample implementation, which follows below. As you want to combine your existing MVC filter with the ability to configure routes for an action, the implementation below implements both IResourceFilter (this can be whatever filter type you're using) and IActionModelConvention:

public class VerifyAccessFilterAttribute : Attribute, IActionModelConvention, IResourceFilter { public VerifyAccessFilterAttribute(params string[] routeTemplates) { RouteTemplates = routeTemplates; } public string[] RouteTemplates { get; set; } public void Apply(ActionModel actionModel) { actionModel.Selectors.Clear(); foreach (var routeTemplate in RouteTemplates) { actionModel.Selectors.Add(new SelectorModel { AttributeRouteModel = new AttributeRouteModel { Template = routeTemplate }, ActionConstraints = { new HttpMethodActionConstraint(new[] { "GET" }) } }); } } public void OnResourceExecuting(ResourceExecutingContext ctx) { ... } public void OnResourceExecuted(ResourceExecutedContext ctx) { ... } }

在此示例中,所有内容均与Apply方法有关,该方法仅添加了一个新的 SelectorModel 用于每个routeTemplate(我已将其命名),每个routeTemplate都限于HTTP GET请求.

In this example, it's all about the Apply method, which simply adds a new SelectorModel for each routeTemplate (as I've named it), each of which is constrained to HTTP GET requests.

更多推荐

在ASP.NET Core 2中创建自定义路由属性

本文发布于:2023-11-14 10:34:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1586978.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   路由   属性   ASP   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!