Skip to content

Instantly share code, notes, and snippets.

@alastairs
Created May 16, 2017 19:31
Show Gist options
  • Select an option

  • Save alastairs/ee6b5d67458ecdff6d29c136167bcd54 to your computer and use it in GitHub Desktop.

Select an option

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
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