Created
March 3, 2019 15:53
-
-
Save explorer14/44d182fc99985ce5d496b89d026ece72 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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