Skip to content

Instantly share code, notes, and snippets.

@explorer14
Created March 3, 2019 15:53
Show Gist options
  • Save explorer14/44d182fc99985ce5d496b89d026ece72 to your computer and use it in GitHub Desktop.
Save explorer14/44d182fc99985ce5d496b89d026ece72 to your computer and use it in GitHub Desktop.
public class ModelBindingFailureFilter : IActionFilter
{
public void OnActionExecuted(ActionExecutedContext context)
{
}
public void OnActionExecuting(ActionExecutingContext context)
{
if (!context.ModelState.IsValid)
{
context
.HttpContext
.Response
.ContentType = "application/problem+json";
var validationProblems = new ValidationProblemDetails(
context.ModelState)
{
Title = "One or more validation errors occurred",
Status = (int)HttpStatusCode.BadRequest,
Instance = context.HttpContext.TraceIdentifier
};
context.Result = new BadRequestObjectResult(validationProblems);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment