Skip to content

Instantly share code, notes, and snippets.

@AlexZeitler
Created February 16, 2012 10:03
Show Gist options
  • Select an option

  • Save AlexZeitler/1843763 to your computer and use it in GitHub Desktop.

Select an option

Save AlexZeitler/1843763 to your computer and use it in GitHub Desktop.
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