4

I have an ASP.NET application using Web API 2.

To force model validation on all actions, I use a filter, like so:

public class ValidateModelAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        if (!actionContext.ModelState.IsValid)
        {
            actionContext.Response = actionContext.Request.CreateErrorResponse(
                HttpStatusCode.BadRequest, actionContext.ModelState);
        }
    }
}

This works well in most cases, but when I execute a POST request against an API-endpoint without any content in the request body, it is as if the model validation does not kick in.

The controller-action takes a viewmodel with three properties - all required strings.

public class AddEntityViewModel
{
    [Required]
    public string Property1 { get; set; }
    [Required]
    public string Property2 { get; set; }
    [Required]
    public string Property3 { get; set; }
}

If I just add some random data as the request body, model validation kick in and deny the request as expected, but if the request body is completely empty, model validation pass and the model I get in my action is null.

Is there a good way to force model validation even if the request body is empty, so that such requests are denied? Or is there some other way to approach this?

Christofer Eliasson
  • 32,939
  • 7
  • 74
  • 103
  • Show the code of your model – Sergey Litvinov Feb 20 '14 at 10:35
  • Just found [this SO question](http://stackoverflow.com/questions/19851352/mvc5-webapi2-modelstate-is-valid-with-null-model) that deals with the exact same problem, so I'm voting to close this question. – Christofer Eliasson Feb 20 '14 at 11:55
  • @ChristoferEliasson if it's your question, can't you just delete it? – Michael Edenfield Feb 20 '14 at 14:51
  • @MichaelEdenfield I could, but as mentioned [in the help section](http://stackoverflow.com/help/privileges/moderator-tools), duplicate questions can often work as a signpost to other useful answers on other questions. As people tend to ask and search using completely different words, the better the coverage, the better the odds fellow programmers can find the answer they’re looking for. Because of this, I voted to close the question as a duplicate, instead of deleting it all together. – Christofer Eliasson Feb 20 '14 at 15:04

1 Answers1

2

What I ended up doing was extending my model validation filter to also verify that the model isn't null.

public class ValidateModelAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        if (!actionContext.ModelState.IsValid)
        {
            actionContext.Response = actionContext.Request.CreateErrorResponse(
                HttpStatusCode.BadRequest, actionContext.ModelState);
        } 
        else if (actionContext.ActionArguments.ContainsValue(null))              
        {
            actionContext.Response = actionContext.Request.CreateErrorResponse(
                HttpStatusCode.BadRequest, "Request body cannot be empty");
        }
    }
}
Christofer Eliasson
  • 32,939
  • 7
  • 74
  • 103