Skip to content

Instantly share code, notes, and snippets.

@mattuu
Created April 10, 2019 07:47
Show Gist options
  • Save mattuu/cf7aaa43a07685b2e93a095ec62056ba to your computer and use it in GitHub Desktop.
Save mattuu/cf7aaa43a07685b2e93a095ec62056ba to your computer and use it in GitHub Desktop.
ValidateModelStateAttribute - adds handing invalid model state and returns BadRequest (400)
public class ValidateModelStateAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext context)
{
if (!context.ModelState.IsValid)
{
context.Result = new BadRequestObjectResult(context.ModelState);
}
base.OnActionExecuting(context);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment