aspnetcore IExceptionFilter

    public class GlobalExceptionFilter : IExceptionFilter
    {
        public void OnException(ExceptionContext context)
        {
            context.Result = new ObjectResult(new { Status = 500, Msg = context.Exception.Message });
        }
    }
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();

            services.AddMvc(op =>
            {
                op.Filters.Add(typeof(GlobalExceptionFilter));
            })
            .AddJsonOptions(op =>
            {
                op.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
                op.JsonSerializerOptions.PropertyNamingPolicy = null;// 取消默认首字母大写
            });
        }

 

posted on 2020-09-16 22:21  jonney_wang  阅读(238)  评论(0编辑  收藏  举报

导航