#region Add global exception filter and parameter validation filter services.AddControllers(options => { options.Filters.
Add<ValidFilterAttribute>(); options.Filters.Add<GlobalExceptionFilter>(); }); #
endregion
ValidFilterAttribute
// Parameter verification 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
// Global error exception check 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); } } }

Technology
©2019-2020 Toolsou All rights reserved,
hive compress &&hdfs Merge small files I've been drinking soft water for three years ? What is the use of soft water and water softener 《 League of Heroes 》 Mobile game open public test : support iOS/Android Dual platform The situation of receiving and receiving multi-path VaR - Value at risk - Monte Carlo method - Pythonnumpy: The creation of multidimensional array uniapp Summary of page value transfer use C Language makes a very simple airplane game Docker Container data volume ,DockerfileJavaScript Medium Call and Apply