参考:
- https://code-maze.com/action-filters-aspnetcore/
- https://github.com/aspnet/AspNetCore.Docs/issues/12157
- https://docs.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-3.0#automatic-http-400-responses
-
关闭默认模型验证过滤器
[ApiController] 默认自带有400模型验证,且优先级比较高,如果需要自定义模型验证,则需要先关闭默认的模型验证
在StartUp.cs 中的MVC服务配置修改#region MVC services.AddMvc(o => { }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2) .ConfigureApiBehaviorOptions(o => { o.SuppressModelStateInvalidFilter = true; }); #endregion
-
首先定义统一输出的格式为
{ code: errors:[ filed: message: ] }
-
添加自定义模型验证DTO
public class ErrorResultDTO { /// <summary> /// 参数领域 /// </summary> public string Field { get; set; } /// <summary> /// 错误信息 /// </summary> public string Message { get; set; } }
-
添加过滤器
过滤器继承自:ActionFilterAttribute, IActionFilterpublic class ModelActionFiter :ActionFilterAttribute, IActionFilter { public ModelActionFiter() { } public override void OnActionExecuted(ActionExecutedContext context) { } public override void OnActionExecuting(ActionExecutingContext context) { if (!context.ModelState.IsValid) { List<ErrorResultDTO> errorResults = new List<ErrorResultDTO>(); foreach (var item in context.ModelState) { var result = new ErrorResultDTO { Field = item.Key, Msg = "", }; foreach (var error in item.Value.Errors) { if (!string.IsNullOrEmpty(result.Msg)) { result.Msg += '|'; } result.Msg += error.ErrorMessage; } errorResults.Add(result); } context.Result = new BadRequestObjectResult(new { Code = StatusCodes.Status400BadRequest, Errors = errorResults }); } } }
-
将写的过滤器注册到全局
services.AddMvc(o => { o.Filters.Add(new ModelActionFiter()); })
结果测试
- 扩展
- 利用拦截器来测试action 的运行时间,记录日志,并设置时间限制,超过某个时间段自动发送邮件通知维护人员