ABP 替换异常过滤器

public class ExceptionFilter: AbpExceptionFilter
    {
        public ExceptionFilter(IErrorInfoBuilder errorInfoBuilder, IAbpAspNetCoreConfiguration configuration) : base(errorInfoBuilder, configuration)
        { 
        
        }
        protected override int GetStatusCode(ExceptionContext context, bool wrapOnError)
        {
            if (context.Exception is AbpAuthorizationException)
            {
                if (!context.HttpContext.User.Identity.IsAuthenticated)
                {
                    return 401;
                }
                return 403;
            }
            if (context.Exception is AbpValidationException)
            {
                return 400;
            }
            if (context.Exception is EntityNotFoundException)
            {
                return 404;
            }
            if (context.Exception is UserFriendlyException)
            {
                return 10002;
            }
            if (wrapOnError)
            {
                return 500;
            }
            return context.HttpContext.Response.StatusCode;
        }

    }
 services.AddMvc(
                options => { 
                    options.Filters.Add(new CorsAuthorizationFilterFactory(_defaultCorsPolicyName));
                    options.Filters.AddService(typeof(ExceptionFilter), order: 2);
                }

            ).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
 return services.AddAbp<WebSystemWebHostModule>(
                // Configure Log4Net logging
                options =>
                {
                    options.IocManager.IocContainer.AddFacility<LoggingFacility>(
            f => f.UseAbpLog4Net().WithConfig("log4net.config")
        );
                    options.IocManager.Register<IExceptionFilter, ExceptionFilter>(Abp.Dependency.DependencyLifeStyle.Transient);
                }
            );

 

posted on 2020-04-27 14:08  落叶子  阅读(894)  评论(0编辑  收藏  举报

导航