思路:在startup.cs的configservice中配置一下,异常实现 ExceptionFilterAttribute 接口
1 public class ExceptionConfig 2 { 3 public static void ConfigureServices(IServiceCollection services) 4 { 5 //自定义捕捉异常 6 services.AddControllersWithViews( 7 options => options.Filters.Add(typeof(CustomerExceptionFilterAttribute)) 8 ); 9 10 } 11 12 public static void Configure(IApplicationBuilder app, IWebHostEnvironment env) 13 { 14 15 16 } 17 }
1 public class CustomerExceptionFilterAttribute : ExceptionFilterAttribute 2 { 3 4 public CustomerExceptionFilterAttribute() { } 5 6 7 public override void OnException(ExceptionContext context) 8 { 9 // base.OnException(context); 10 if (!context.ExceptionHandled) 11 { 12 13 context.Result = new JsonResult(new 14 { 15 Result = false, 16 Msg = "请联系开发", 17 Debug = context.Exception.Message 18 }); 19 20 context.ExceptionHandled = true; 21 }; 22 } 23 24 }
标签:core,自定义,void,CustomerExceptionFilterAttribute,context,net,异常,public From: https://www.cnblogs.com/hanliping/p/18369894