Created
October 12, 2020 07:52
-
-
Save kid-cavaquinho/c0cdfb6271dad650588e6a2f4c692fa7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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