pootzko
3/18/2015 - 1:47 PM

Validation action filter as controller action attribute will return bad request upon view model validation failed

Validation action filter as controller action attribute will return bad request upon view model validation failed

public class ValidationActionFilter : ActionFilterAttribute
{
    // SEE: http://stackoverflow.com/questions/11686690/handle-modelstate-validation-in-asp-net-web-api
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        var modelState = actionContext.ModelState;

        if (!modelState.IsValid)
        {
            actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, modelState);
        }

        //base.OnActionExecuting(actionContext);
    }
}
[HttpPost]
[ValidationActionFilter]
public HttpResponseMessage Approve(SomeModel model)
{
    return this.Request.CreateResponse(HttpStatusCode.OK);
}