Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.