Skip to content

Instantly share code, notes, and snippets.

Created June 3, 2012 02:58
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
A filter for ASP.NET Web API to return 400 upon invalid model binding
using System;
using System.Net;
using System.Net.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class InvalidModelStateFilterAttribute : ActionFilterAttribute
public override void OnActionExecuting(HttpActionContext actionContext)
if (!actionContext.ModelState.IsValid)
actionContext.Response = actionContext.Request.CreateErrorResponse(
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment