翼度科技»论坛 编程开发 .net 查看内容

.Net Core ActionFilter

7

主题

7

帖子

21

积分

新手上路

Rank: 1

积分
21
目录

作用



  • 在请求AuthorizeFilter -> ResourceFilter -> ActionFilter, 可以记录Action的日志
  • ActionFilter 在控制器实例化之后执行
  • ResourceFilter 可以在全局, Controller, Action 都可以设置, 并且都会执行(一个ResourceFilter可以重复设置)
如果都设置执行顺序为:

  • 全局
  • Controller
  • Action
  • Action 方法
  • Action
  • Controller
  • 全局
实现

IActionFilter


  • 需要继承 Attribute 并 并实现 IActionFilter
  • 实现接口方法
执行顺序为:

  • OnActionExecuting
  • Action
  • OnActionExecuted
IAsyncActionFilter


  • 需要继承 Attribute 并 并实现 IAsyncActionFilter
  • 实现接口方法
  • 该接口只提供一个 OnActionExecutionAsync方法,如果想执行ActionExecutedContext方法,需要执行方法中ActionExecutionDelegate委托并取返回值然后代码在执行为
    ActionExecutedContext方法
执行顺序为:

  • OnActionExecuting
  • Action
  • OnActionExecuted
ActionFilterAttribute


  • 需要继承 ActionFilterAttribute
  • 重写 OnActionExecuting OnActionExecuted OnResultExecuting OnResultExecuted 方法
执行顺序为:

  • OnActionExecuting
  • Action
  • OnActionExecuted
  • OnResultExecuting
  • OnResultExecuted
Aop Action执行

过滤器代码
  1. using Microsoft.AspNetCore.Mvc.Filters;
  2. namespace Cnpc.Com.Ioc.WebApp.Filter.ActionFilter
  3. {
  4.     public class CustomAsyncActionFilter : Attribute, IAsyncActionFilter
  5.     {
  6.         public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
  7.         {
  8.             {
  9.                 Console.WriteLine("ActionExecutingContext.....");
  10.             }
  11.             ActionExecutedContext executed = await next();
  12.             {
  13.                 Console.WriteLine("ActionExecutedContext.....");
  14.             }
  15.         }
  16.     }
  17. }
复制代码
Action代码
  1. using Cnpc.Com.Ioc.WebApp.Filter;
  2. using Microsoft.AspNetCore.Mvc;
  3. namespace Cnpc.Com.Ioc.WebApp.Controllers
  4. {
  5.     [Route("api/[controller]/[action]")]
  6.     [ApiController]
  7.     public class TestFilterController : ControllerBase
  8.     {
  9.         //[TypeFilter(typeof(CustomAsyncActionFilter))]  如果想在ActionFilter支持Nlog 并使用构造注入就这样写
  10.         [CustomAsyncActionFilter]
  11.         [HttpGet]
  12.         public async Task Action_AsyncActionFilter()
  13.         {
  14.             Console.WriteLine("Func...");
  15.             await Task.CompletedTask;
  16.         }
  17.     }
  18. }
复制代码
使用日志

Action
  1. //[ServiceFilter(typeof(CustomAsyncActionFilter))] 如果使用SerciceFilter 要先将CustomAsyncActionFilter 注册到ioc中
  2. [TypeFilter(typeof(CustomAsyncActionFilter))] //如果想在ActionFilter支持Nlog 并使用构造注入就这样写
  3. [HttpGet]
  4. public async Task Action_AsyncActionFilter()
  5. {
  6.     Console.WriteLine("Func...");
  7.     await Task.CompletedTask;
  8. }
  9. CustomAsyncActionFilter.cs
  10. using Cnpc.Com.Ioc.IBll;
  11. using Cnpc.Com.Ioc.IDal;
  12. using Microsoft.AspNetCore.Mvc.Filters;
  13. namespace Cnpc.Com.Ioc.WebApp.Filter.ActionFilter
  14. {
  15.     public class CustomAsyncActionFilter : Attribute, IAsyncActionFilter
  16.     {
  17.         ILogger<CustomAsyncActionFilter> logger { get; set; }
  18.         IStudent student { get; set; }
  19.         IWrite write { get;set; }
  20.         public CustomAsyncActionFilter(ILogger<CustomAsyncActionFilter> logger,IStudent student,IWrite write)
  21.         {
  22.             this.logger = logger;
  23.             this.student = student;
  24.             this.write = write;
  25.         }
  26.         public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
  27.         {
  28.             {
  29.                 Console.WriteLine("ActionExecutingContext.....");
  30.                 logger.LogInformation(context.HttpContext.Request.Path + "before..");
  31.                 this.student.DoHomeWork(write);
  32.             }
  33.             ActionExecutedContext executed = await next();
  34.             {
  35.                 Console.WriteLine("ActionExecutedContext.....");
  36.                 logger.LogInformation(context.HttpContext.Request.Path + "after..");
  37.             }
  38.         }
  39.     }
  40. }
复制代码
全局注册

Program.cs
  1. //全局注册
  2. builder.Services.AddControllersWithViews(options =>
  3. {
  4.     options.Filters.Add<CustomAsyncActionFilter>();
  5. });
复制代码
来源:https://www.cnblogs.com/qfccc/archive/2023/07/08/17537763.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具