public sealed class MyModelAttribute : ActionFilterAttribute
{
public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
if (!context.ModelState.IsValid)
{
var errors = context.ModelState.Values.SelectMany(v => v.Errors).Select(m => m.ErrorMessage).ToList();;
}
else
{
if (next != null)
{
await next();
}
}
}
}
即使我的模型有多个错误,这只会返回一个错误。