Created
February 16, 2012 10:03
-
-
Save AlexZeitler/1843763 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 ValidationActionFilter : ActionFilterAttribute | |
| { | |
| public override void OnActionExecuting(HttpActionContext context) | |
| { | |
| var modelState = context.ModelState; | |
| if (!modelState.IsValid) | |
| { | |
| dynamic errors = new JsonObject(); | |
| foreach (var key in modelState.Keys) | |
| { | |
| var state = modelState[key]; | |
| if (state.Errors.Any()) | |
| { | |
| dynamic jsonResult = new JsonObject(); | |
| errors[key] = state.Errors.First().ErrorMessage; | |
| } | |
| } | |
| context.Response = new HttpResponseMessage<JsonValue>(errors, HttpStatusCode.BadRequest); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment