Created
May 16, 2017 19:31
-
-
Save alastairs/ee6b5d67458ecdff6d29c136167bcd54 to your computer and use it in GitHub Desktop.
A simple ASP.NET MVC FilterAttribute that validates the model in the request pipeline
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
| namespace Mvc.Examples | |
| { | |
| internal class ValidateModelAttribute : ActionFilterAttribute | |
| { | |
| public override void OnActionExecuting(ActionExecutingContext actionContext) | |
| { | |
| if (actionContext.ModelState.IsValid == false) | |
| { | |
| actionContext.Result = new BadRequestObjectResult( | |
| actionContext.ModelState.Values | |
| .SelectMany(e => e.Errors) | |
| .Select(e => e.ErrorMessage)); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment