注:带有注入的扩展类,在标记时需要用
[TypeFilter(typeof(CustomAsyncActionFilterAttribute))] 或者
[ServiceFilter(typeof(CustomAsyncActionFilterAttribute))]
扩展类CustomActionFilterAttribute
using Microsoft.AspNetCore.Mvc.Filters; namespace Demo02.Utility.Filters { /// <summary> /// ActionFilter /// </summary> public class CustomActionFilterAttribute : Attribute,IActionFilter { private readonly ILogger<CustomActionFilterAttribute> _logger; /// <summary> /// 构造函数 /// </summary> /// <param name="logger"></param> public CustomActionFilterAttribute(ILogger<CustomActionFilterAttribute> logger) { _logger = logger; } /// <summary> /// 在方法执行之后 /// </summary> /// <param name="context"></param> public void OnActionExecuted(ActionExecutedContext context) { _logger.LogInformation("CustomActionFilterAttribute.OnActionExecuted"); } /// <summary> /// 在方法执行之前 /// </summary> /// <param name="context"></param> public void OnActionExecuting(ActionExecutingContext context) { var controllerName = context.RouteData.Values["controller"]; var actionName = context.RouteData.Values["action"]; var queryString = context.HttpContext.Request.QueryString.ToString(); _logger.LogInformation($"CustomActionFilterAttribute.OnActionExecuting====controllerName:{controllerName},actionName:{actionName},queryString{queryString}"); } } }
Controller Action测试
/// <summary> /// GET AOP测试 AsyncActionFilter日志 /// </summary> /// <returns></returns> [HttpGet()] [Route("ActionFilter/CustomAsyncActionFilter/{id:int}")] [TypeFilter(typeof(CustomAsyncActionFilterAttribute))] //[ServiceFilter(typeof(CustomAsyncActionFilterAttribute))] public IActionResult AsyncGetLog(int id) { return new JsonResult(new ApiResut<String>() { Success = true, Data = "AOP AsyncActionFilter 测试", Time = DateTime.Now.ToString() }); }
标签:CustomActionFilterAttribute,IActionFilter,typeof,context,CustomAsyncActionFilter From: https://www.cnblogs.com/yyd-sun/p/17065653.html