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;// 取消默认首字母大写 }); }
原文:https://www.cnblogs.com/jonney-wang/p/13681808.html