#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,
about String How to create objects JavaScript Hundred refining into Immortals 1.15 Legendary Is MCU embedded , It's a cliche Resume the 13th session python Blue Bridge Cup html+css+js Make a simple website home page java Connect to the database to realize basic addition, deletion, modification and query VHDL——JK trigger Java of JDBC programming 3 4j It's not legal python expression _3+4j It's not legal Python expression .【linux】shell: ordinary shell Script exercise