Ultimate ASP.NET CORE 6.0 Web API --- 读书笔记(15)
2022/6/17 1:20:12
本文主要是介绍Ultimate ASP.NET CORE 6.0 Web API --- 读书笔记(15),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
15 Action Filters
本文内容来自书籍: Marinko Spasojevic - Ultimate ASP.NET Core Web API - From Zero To Six-Figure Backend Developer (2nd edition)
- Authorization filters,放在最前面,决定了请求是否有权限访问
- Resource filters,就在授权过滤器的后面,在
缓存
和性能
很有用 - Action filters,在
action
的前后执行 - Exception filters,在填充响应体之前,处理异常
- Result filters,它们在操作方法结果执行之前和之后运行
这个章节只介绍Action filter
要创建Action filter
,有几种不同实现
- IActionFilter
- IAsyncActionFilter
- ActionFilterAttribute
如果是同步方式实现,需要实现OnActionExecuting
和OnActionExecuted
public class ActionFilterExample : IActionFilter { public void OnActionExecuting(ActionExecutingContext context) { // our code before action executes } public void OnActionExecuted(ActionExecutedContext context) { // our code after action executes } }
如果是异步方式实现,需要实现OnActionExecutionAsync
public class AsyncActionFilterExample : IAsyncActionFilter { public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { // execute any code before the action executes var result = await next(); // execute any code after the action executes } }
15.2 The Scope of Action Filters
过滤器有几种范围
- Global
// Program.cs builder.Services.AddControllers(config => { config.Filters.Add(new GlobalFilterExample()); });
- Action
- Controller
// Program.cs builder.Services.AddScoped<ActionFilterExample>(); builder.Services.AddScoped<ControllerFilterExample>(); // Controller [ServiceFilter(typeof(ControllerFilterExample))] [Route("api/[controller]")] [ApiController] public class TestController : ControllerBase { [HttpGet] [ServiceFilter(typeof(ActionFilterExample))] public IEnumerable<string> Get() { return new string[] { "example", "data" }; } }
15.3 Order of Invocation
这是默认的执行顺序,也可以通过添加order
属性,来改变执行顺序
[ServiceFilter(typeof(ControllerFilterExample), Order = 2)] [Route("api/[controller]")] [ApiController] public class TestController : ControllerBase { [HttpGet] [ServiceFilter(typeof(ActionFilterExample), Order = 1)] public IEnumerable<string> Get() { return new string[] { "example", "data" }; } }
15.5 Validation with Action Filters
前面的章节介绍过,我们的业务逻辑代码中,没有try-catch
块,都提取到全局异常处理的中间件了,但是现在可以使用Action filter
来更进一步处理
现在从POST
和PUT
的逻辑代码中,可以看到相似的验证逻辑
if (company is null) return BadRequest("CompanyForUpdateDto object is null"); if (!ModelState.IsValid) return UnprocessableEntity(ModelState);
然后我们可以将验证逻辑提取出来,重复使用
public class ValidationFilterAttribute : IActionFilter { public ValidationFilterAttribute() { } public void OnActionExecuting(ActionExecutingContext context) { var action = context.RouteData.Values["action"]; var controller = context.RouteData.Values["controller"]; var param = context.ActionArguments .SingleOrDefault(x => x.Value.ToString().Contains("Dto")).Value; if (param is null) { context.Result = new BadRequestObjectResult($"Object is null. Controller:{controller}, action: {action}"); return; } if (!context.ModelState.IsValid) context.Result = new UnprocessableEntityObjectResult(context.ModelState); } public void OnActionExecuted(ActionExecutedContext context) { } }
这篇关于Ultimate ASP.NET CORE 6.0 Web API --- 读书笔记(15)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#
- 2024-01-24Advanced .Net Debugging 1:你必须知道的调试工具
- 2024-01-24.NET集成IdGenerator生成分布式全局唯一ID
- 2024-01-23用CI/CD工具Vela部署Elasticsearch + C# 如何使用
- 2024-01-23.NET开源的简单、快速、强大的前后端分离后台权限管理系统