返回顶部

.Net Core Aop之IActionFilter

一、简介

在.net core 中Filter分为以下六大类:

1、AuthorizeAttribute(权限验证)

2、IResourceFilter(资源缓存)

3、IActionFilter(执行方法前后的记录)

4、IResultFilter(结果生成前后扩展)

5、IAlwaysRun(响应结果的补充)

6、IExceptionFilter(异常处理)

 二、IActionFilter(同步)

1、定义Filter

public class CustomerActionFilterAttribute : Attribute, IActionFilter
    {
        /// <summary>
        /// 在XXAction执行之前
        /// </summary>
        /// <param name="context"></param>
        public void OnActionExecuting(ActionExecutingContext context)
        {
            Console.WriteLine("CustomerActionFilterAttribute.OnActionExecuting");
        }

        /// <summary>
        /// 在XXAction执行之后
        /// </summary>
        /// <param name="context"></param>
        public void OnActionExecuted(ActionExecutedContext context)
        {
            Console.WriteLine("CustomerActionFilterAttribute.OnActionExecuted");
        }

        
    }

 

2、使用Filter

public class Home1Controller : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public Home1Controller(ILogger<HomeController> logger)
        {
            _logger = logger;
            Console.WriteLine($"执行 {this.GetType().Name} 构造函数");
        }


        [CustomerActionFilter]
        public IActionResult Index()
        {
            Console.WriteLine($"执行 Index 方法");
            return View();
        }
    }

 3、扩展记录日志

public class CustomerActionFilterAttribute : Attribute, IActionFilter
    {

        private readonly ILogger<CustomerActionFilterAttribute> _logger;

        public CustomerActionFilterAttribute(ILogger<CustomerActionFilterAttribute> logger)
        {
            _logger = logger;
        }
        /// <summary>
        /// 在XXAction执行之前
        /// </summary>
        /// <param name="context"></param>
        public void OnActionExecuting(ActionExecutingContext context)
        {
            var para = context.HttpContext.Request.QueryString.Value;
            var controllerName = context.ActionDescriptor.RouteValues["controller"];
            var actionName = context.ActionDescriptor.RouteValues["action"];

            _logger.LogInformation($"当前请求的控制器是{controllerName},方法名{actionName},请求参数是{JsonConvert.SerializeObject(para)}");

        }

        /// <summary>
        /// 在XXAction执行之后
        /// </summary>
        /// <param name="context"></param>
        public void OnActionExecuted(ActionExecutedContext context)
        {
            var para = context.Result;
            var controllerName = context.ActionDescriptor.RouteValues["controller"];
            var actionName = context.ActionDescriptor.RouteValues["action"];
            _logger.LogInformation($"当前请求的控制器是{controllerName},方法名{actionName},请求结果是{JsonConvert.SerializeObject(para)}");

        }
    }
public class Home1Controller : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public Home1Controller(ILogger<HomeController> logger)
        {
            _logger = logger;
            Console.WriteLine($"执行 {this.GetType().Name} 构造函数");
        }


        [TypeFilter(typeof(CustomerActionFilterAttribute))]
        public IActionResult Index(int id)
        {
            Console.WriteLine($"执行 Index 方法");
            ViewBag.Data = new { name = "11111" };
            return View();
        }
    }

 

 

 三、IAsyncActionFilter(异步)

public class CustomerAsyncActionFilterAttribute : Attribute, IAsyncActionFilter
    {

        private readonly ILogger<CustomerAsyncActionFilterAttribute> _logger;

        public CustomerAsyncActionFilterAttribute(ILogger<CustomerAsyncActionFilterAttribute> logger)
        {
            _logger = logger;
        }
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {

            var para = context.HttpContext.Request.QueryString.Value;
            var controllerName = context.ActionDescriptor.RouteValues["controller"];
            var actionName = context.ActionDescriptor.RouteValues["action"];

            _logger.LogInformation($"当前请求的控制器是{controllerName},方法名{actionName},请求参数是{JsonConvert.SerializeObject(para)}");
            
            var excuteContext = await next.Invoke(); // 这句话就是去执行Action

            _logger.LogInformation($"当前请求的控制器是{controllerName},方法名{actionName},请求结果是{JsonConvert.SerializeObject(excuteContext.Result)}");

        }
    }
public class Home1Controller : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public Home1Controller(ILogger<HomeController> logger)
        {
            _logger = logger;
            Console.WriteLine($"执行 {this.GetType().Name} 构造函数");
        }


        //[TypeFilter(typeof(CustomerActionFilterAttribute))]
        [TypeFilter(typeof(CustomerAsyncActionFilterAttribute))]
        public IActionResult Index(int id)
        {
            Console.WriteLine($"执行 Index 方法");
            ViewBag.Data = new { name = "11111" };
            return View();
        }
    }

 

 

 

 

四、总结

作用:该Filter可以记录Action执行前后的参数和结果

执行顺序:
1、执行控制器的构造函数
2、执行CustomerActionFilterAttribute.OnActionExecuting
3、执行Action方法
4、执行CustomerActionFilterAttribute.OnActionExecuted

 

posted @ 2022-02-21 22:47  SportSky  阅读(1371)  评论(0编辑  收藏  举报
作者:SportSky 出处: http://www.cnblogs.com/sportsky/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。如果觉得还有帮助的话,可以点一下右下角的【推荐】,希望能够持续的为大家带来好的技术文章!想跟我一起进步么?那就【关注】我吧。