ToB企服应用市场:ToB评测及商务社交产业平台

标题: .Net Core ActionFilter [打印本页]

作者: 慢吞云雾缓吐愁    时间: 2023-8-7 18:09
标题: .Net Core ActionFilter

目录

作用


执行顺序为:
实现

IActionFilter


IAsyncActionFilter


ActionFilterAttribute


Demo

CustomAsyncActionFilter.cs
  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. }
复制代码
TestFilterController.cs
  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. }
复制代码
如何在Actionfilter使用日志

Action.cs
  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. }
复制代码
CustomAsyncActionFilter.cs
  1. using Cnpc.Com.Ioc.IBll;
  2. using Cnpc.Com.Ioc.IDal;
  3. using Microsoft.AspNetCore.Mvc.Filters;
  4. namespace Cnpc.Com.Ioc.WebApp.Filter.ActionFilter
  5. {
  6.     public class CustomAsyncActionFilter : Attribute, IAsyncActionFilter
  7.     {
  8.         ILogger<CustomAsyncActionFilter> logger { get; set; }
  9.         IStudent student { get; set; }
  10.         IWrite write { get;set; }
  11.         public CustomAsyncActionFilter(ILogger<CustomAsyncActionFilter> logger,IStudent student,IWrite write)
  12.         {
  13.             this.logger = logger;
  14.             this.student = student;
  15.             this.write = write;
  16.         }
  17.         public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
  18.         {
  19.             {
  20.                 Console.WriteLine("ActionExecutingContext.....");
  21.                 logger.LogInformation(context.HttpContext.Request.Path + "before..");
  22.                 this.student.DoHomeWork(write);
  23.             }
  24.             ActionExecutedContext executed = await next();
  25.             {
  26.                 Console.WriteLine("ActionExecutedContext.....");
  27.                 logger.LogInformation(context.HttpContext.Request.Path + "after..");
  28.             }
  29.         }
  30.     }
  31. }
复制代码
全局注册

Program.cs
  1. //全局注册
  2. builder.Services.AddControllersWithViews(options =>
  3. {
  4.     options.Filters.Add<CustomAsyncActionFilter>();
  5. });
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4