#region 添加全局异常过滤器和参数校验过滤器 services.AddControllers(options => { options.Filters.
Add<ValidFilterAttribute>(); options.Filters.Add<GlobalExceptionFilter>(); }); #
endregion
ValidFilterAttribute
//参数校验 public class ValidFilterAttribute : BaseActionFilterAsync { public
override async Task OnActionExecuting(ActionExecutingContext context) { if (!
context.ModelState.IsValid) { var msgList = context.ModelState.Values.SelectMany
(x => x.Errors).Select(x => x.ErrorMessage); context.Result = Error(string.Join(
",", msgList)); } await Task.CompletedTask; } }
GlobalExceptionFilter
//全局错误异常校验 public class GlobalExceptionFilter : BaseActionFilterAsync,
IAsyncExceptionFilter{ readonly ILogger _logger; public GlobalExceptionFilter(
ILogger<GlobalExceptionFilter> logger) { _logger = logger; } public async Task
OnExceptionAsync(ExceptionContext context) { Exception ex = context.Exception;
if (ex is BusException busEx) { _logger.LogInformation(busEx.Message); context.
Result= Error(busEx.Message, busEx.ErrorCode); } else { _logger.LogError(ex, "")
; context.Result = Error(ex.Message); } await Task.CompletedTask; } }
BaseActionFilterAsync
public abstract class BaseActionFilterAsync : Attribute, IAsyncActionFilter {
public async virtual Task OnActionExecuting(ActionExecutingContext context) {
await Task.CompletedTask; } public async virtual Task OnActionExecuted(
ActionExecutedContext context) { await Task.CompletedTask; } public async Task
OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate
next) { await OnActionExecuting(context); if (context.Result == null) { var
nextContext= await next(); await OnActionExecuted(nextContext); } } }

技术
©2019-2020 Toolsou All rights reserved,
Qt学习2——.pro文件和.h文件介绍LinkedHashMap基本用法&使用实现简单缓存pytorch之ResNet18(对cifar10数据进行分类准确度达到94%)华为Mate 40 Pro+ 5G曝光:徕卡电影镜头、陶瓷机身统计字母个数(java语言实现)JavaScript中的 Call 和 Apply1190 反转每对括号间的子串 leetcode记一次EventBus内存泄露导致的项目问题浅谈uni-app页面传值问题 Chrome OS,对程序员和Windows意味着什么?,互联网营销