I have an ASP.NET application using Web API 2.
To force a model check for all actions, I use a filter, for example:
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 the API endpoint without any content in the request body, it looks like the model check does not work.
The action controller accepts a model with three properties — all the necessary lines.
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, the model check will hit and reject the request as expected, but if the request body is completely empty, passing the model check and the model I get in my action are zero.
, , ? ?