.net core(.net 6) IResourceFilter 的基础使用(IAsyncResourceFilter)
a 定义CustomResourceFilterAttribute扩展类,继承父类Attribute,实现 IResourceFilter接口
b 实现接口方法
c 标记在API action上
1、CustomResourceFilterAttribute扩展类
Attribute类为标记类。
IResourceFilter接口包括OnResourceExecuted、OnResourceExecuting两个方法;OnResourceExecuting在API执行前执行,OnResourceExecuted在API执行后执行。
using Microsoft.AspNetCore.Mvc.Filters; using System; namespace Demo02.Utility.Filters { /// <summary> /// 自定义ResourceFilter标记类 /// </summary> public class CustomResourceFilterAttribute : Attribute, IResourceFilter { /// <summary> /// API执行后 执行 /// </summary> /// <param name="context"></param> /// <exception cref="NotImplementedException"></exception> public void OnResourceExecuted(ResourceExecutedContext context) { Console.WriteLine("======执行CustomResourceFilterAttribute.OnResourceExecuted===="); } /// <summary> /// API执行前 执行 /// </summary> /// <param name="context"></param> /// <exception cref="NotImplementedException"></exception> public void OnResourceExecuting(ResourceExecutingContext context) { Console.WriteLine("======执行CustomResourceFilterAttribute.OnResourceExecuting===="); } } }
2、在action上标记并测试
在action上标记‘CustomResourceFilterAttribute‘,Attribute可省。
using Demo02.Model; using Demo02.Utility.Filters; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace Demo02.Controllers { /// <summary> /// Filters /// </summary> [ApiVersion("1.0")] [Route("[controller]/V{version:apiVersion}")] [ApiController] public class FiltersController : ControllerBase { private readonly ILogger<FiltersController> _logger; /// <summary> /// 构造函数 /// </summary> /// <param name="logger">控制台日志</param> public FiltersController(ILogger<FiltersController> logger) { _logger = logger; } /// <summary> /// GET AOP测试 ResourceFilter /// </summary> /// <returns></returns> [HttpGet()] [Route("ResourceFilter/CustomResourceFilter")] [CustomResourceFilter] public IActionResult GetString() { return new JsonResult(new ApiResut<String>() { Success = true, Data = "AOP ResourceFilter 测试", Time = DateTime.Now.ToString() }); } } }
执行测试:
3、IAsyncResourceFilter(异步)
IAsyncResourceFilter,实现步骤同IResourceFilter,只是接口不同实现方法不同。
CustomAsyncResourceFilterAttribute 类
using Microsoft.AspNetCore.Mvc.Filters; using System; namespace Demo02.Utility.Filters { /// <summary> /// 自定义AsyncResourceFilter标记类 /// </summary> public class CustomAsyncResourceFilterAttribute : Attribute, IAsyncResourceFilter { /// <summary> /// API异步action 执行前后 /// </summary> /// <param name="context"></param> /// <param name="next"></param> /// <returns></returns> public async Task OnResourceExecutionAsync(ResourceExecutingContext context, ResourceExecutionDelegate next) { Console.WriteLine("======Before======CustomAsyncResourceFilterAttribute.OnResourceExecutionAsync================"); await next.Invoke(); Console.WriteLine("======After======CustomAsyncResourceFilterAttribute.OnResourceExecutionAsync================"); } } }
测试API Action
/// <summary> /// GET AOP测试 AsyncResourceFilter /// </summary> /// <returns></returns> [HttpGet()] [Route("ResourceFilter/CustomAsyncResourceFilter")] [CustomAsyncResourceFilter] public IActionResult GetSomeThing() { return new JsonResult(new ApiResut<String>() { Success = true, Data = "AOP AsyncResourceFilter 测试" }); }