# 记录一下 DotNET6 WebAPI 全局异常处理
# 通常接口出现一些小错误,页面会返回一堆看不懂的代码
# 这对于我们寻找错误并没有什么太大的帮助,反而看着很难受
# 遇到这种情况我们可以写一个全局异常过滤器,接口报错直接跳到过滤器
# 首先新建一个 ExceptionFilter 类,继承至 ExceptionFilterAttribute
# 代码如下
/// <summary> | |
/// 全局异常过滤器 | |
/// </summary> | |
public class ExceptionFilter : ExceptionFilterAttribute | |
{ | |
private readonly ILogger<ExceptionFilter> _logger; | |
/// <summary> | |
/// 构造函数注入 | |
/// </summary> | |
/// <param name="logger"></param> | |
public ExceptionFilter(ILogger<ExceptionFilter> logger) | |
{ | |
_logger = logger; | |
} | |
/// <summary> | |
/// 全局捕获异常方法 | |
/// </summary> | |
/// <param name="context"></param> | |
public override void OnException(ExceptionContext context) | |
{ | |
if (!context.ExceptionHandled) | |
{ | |
context.Result = new JsonResult(new { Code = 500, Message = context.Exception.Message, Data = "接口发生错误" }); | |
string ActionRoute = ((Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor)context.ActionDescriptor).DisplayName; | |
_logger.LogError("请求路径:{0},错误信息:{1}", ActionRoute, context.Exception.Message); | |
context.ExceptionHandled = true; | |
} | |
} | |
} |
# 请求拦截器
/// <summary> | |
/// 请求拦截器 | |
/// </summary> | |
public class GlobalActionFilter : IActionFilter | |
{ | |
private readonly ILogger<GlobalActionFilter> _logger; | |
public GlobalActionFilter(ILogger<GlobalActionFilter> logger) | |
{ | |
_logger = logger; | |
} | |
/// <summary> | |
/// 执行方法前 | |
/// </summary> | |
/// <param name="context"></param> | |
public void OnActionExecuting(ActionExecutingContext context) | |
{ | |
// 执行方法前先执行这 | |
_logger.LogInformation("执行前"); | |
} | |
/// <summary> | |
/// 执行方法后 | |
/// </summary> | |
/// <param name="context"></param> | |
public void OnActionExecuted(ActionExecutedContext context) | |
{ | |
// 执行方法后执行这 | |
_logger.LogInformation("执行后"); | |
} | |
} |
# 然后在 Program 类里面全局配置
var builder = WebApplication.CreateBuilder(args); | |
// 异常处理 | |
builder.Services.AddMvcCore(options => | |
{ | |
// 异常过滤器 | |
options.Filters.Add<ExceptionFilter>(); | |
// Action 拦截器 | |
options.Filters.Add<GlobalActionFilter>(); | |
}).AddJsonOptions(options => | |
{ | |
// 配置 Json 序列化大小写处理 | |
options.JsonSerializerOptions.PropertyNamingPolicy = null; | |
options.JsonSerializerOptions.DictionaryKeyPolicy = null; | |
// 解决中文被编码 | |
options.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All); | |
}); |