我想添加一个自定义错误响应(而不是使用通用错误响应进行响应)
我向OnActionExecuting添加了一个过滤器,但是此代码在返回错误之前从未执行过? (如果modelstate无效,那么它将仅返回错误列表),我在做什么错了?
https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/filters?view=aspnetcore-2.2
public class CustomResponseValidationFilter : IActionFilter
{
public async Task OnActionExecutionAsync(
ActionExecutingContext context,
ActionExecutionDelegate next)
{
//do something before the action executes
if (context.ModelState.IsValid == false)
{
}
var resultContext = await next();
// do something after the action executes; resultContext.Result will be set
}
}
services.AddMvc(
config =>
{
config.Filters.Add(new CustomResponseValidationFilter());
});