ASP.NET Core-异常处理

ASP.NET Core中异常处理的几种方法:

1、使用开发人员异常页面(只在开发环境中使用此方式)

2、配置HTTP错误代码页(生产环境使用)

3、MVC异常过滤器ExceptionFilter(只能捕获Controller中的异常)

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler(new ExceptionHandlerOptions()
                {
                    //ExceptionHandlingPath = "/Home/Error",
                    ExceptionHandler = (ctx) =>
                    {
                        var feature = ctx.Features.Get<IExceptionHandlerFeature>();
                        var exception = feature.Error;
                        ctx.Response.Redirect("/Home/Error");
                        return Task.CompletedTask;
                    }
                });
            }

参考:

https://www.jianshu.com/p/cab597211136

https://www.jb51.net/article/153926.htm

猜你喜欢

转载自www.cnblogs.com/fanfan-90/p/12526443.html