ASP.Net Core 3.1 AOP Filter

五类:Authorization Filter、 Resource Filter、 Action Filter 、Exception Filter 、Result Filter

新建文件:CustomExecptionFilterAttribute.cs 

public class CustomExecptionFilterAttribute : ExceptionFilterAttribute
    { 
        /// <summary>
        /// 当异常发生时  进来处理
        /// </summary>
        /// <param name="context"></param>
        public override void OnException(ExceptionContext context)
        {
            if (!context.ExceptionHandled)//当异常未处理
            {
                Console.WriteLine($"{context.HttpContext.Request.Path} {context.Exception.Message}"); 
                context.Result = new JsonResult(new
                {
                    Result = false,
                    Msg = "发生异常,请联系管理员"
                });
                context.ExceptionHandled = true;//异常标记-》已处理
            }
        }
    }

Filter三种注册方式:

  1. [CustomExecptionFilterAttribute]   Action注册--》 仅方法生效 
  2. [CustomExecptionFilterAttribute]  Controller注册--》仅控制器生效
  3. 全局注册   Startup--ConfigureServices  --》全局生效
    services.AddControllersWithViews(
        option =>
        {
                option.Filters.Add(typeof(CustomExecptionFilterAttribute));//全局注册--错误处理
        }
        );

404错误是住不到的

AOP好处:

  1. 聚焦业务逻辑,轻松扩展功能
  2. 代码复用,集中管理

进阶需求:错误日志想要记录在txt日志文件里

    public class CustomExecptionFilterAttribute : ExceptionFilterAttribute
    {
        private readonly ILogger<CustomExecptionFilterAttribute> _logger;
        public CustomExecptionFilterAttribute(ILogger<CustomExecptionFilterAttribute> logger)
        {
            _logger = logger;
        }

        /// <summary>
        /// 当异常发生时  进来处理
        /// </summary>
        /// <param name="context"></param>
        public override void OnException(ExceptionContext context)
        {
            if (!context.ExceptionHandled)//当异常未处理
            {
                Console.WriteLine($"{context.HttpContext.Request.Path} {context.Exception.Message}");
                _logger.LogError($"{context.HttpContext.Request.Path} {context.Exception.Message}");
                context.Result = new JsonResult(new
                {
                    Result = false,
                    Msg = "发生异常,请联系管理员"
                });
                context.ExceptionHandled = true;//异常标记-》已处理
            } 
        }
    }

CustomExecptionFilterAttribute是一个特性,特性是编译时确定的,构造函数只能传递常量,不能传递变量;

Filter注入方式:

  1. 全局注册 Startup--ConfigureServices   自动注入  控制器不需要注册
  2. [ServiceFilter(typeof(CustomExecptionFilterAttribute))]    控制器注入+Startup-》ConfigureServices  (services.AddTransient<CustomExecptionFilterAttribute>();//注册)
  3. [TypeFilter(typeof(CustomExecptionFilterAttribute))]   控制器注入
  4. IFilterFactory

若文章有错误,请评论或者私信指出,谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值