Skip to content

Instantly share code, notes, and snippets.

@kid-cavaquinho
Created October 12, 2020 07:53
Show Gist options
  • Save kid-cavaquinho/06514e0d80cda8f26b9e43d41e71035a to your computer and use it in GitHub Desktop.
Save kid-cavaquinho/06514e0d80cda8f26b9e43d41e71035a to your computer and use it in GitHub Desktop.
public class ModelBindingFailureFilterSimple : IActionFilter
{
private readonly ILogger _logger;
public ModelBindingFailureFilterSimple(ILogger logger)
{
_logger = logger;
}
public void OnActionExecuted(ActionExecutedContext context)
{
}
public void OnActionExecuting(ActionExecutingContext context)
{
if (!context.ModelState.IsValid)
{
var modelErrors = context
.ModelState
.ToDictionary(
x => x.Key,
y => y.Value.Errors.Select(
z => z.Exception.Message).ToList());
_logger.Error(
"Model validation failed with following errors {@errors}",
modelErrors);
context.Result = new BadRequestObjectResult(modelErrors);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment