We're developing a Web API using ASP.NET MVC Web API, .NET 4, we're using JSON.NET v 6.0.0.0
If I submit a request to our API such as:
/api/products/lookup?productIdList=&productIdList=&productIdList=
and the request model is defined as
[JsonObject]
public class ProductLookupRequest
{
[Required(ErrorMessage = "productIdList is a required parameter.")]
[MinLength(1, ErrorMessage = "A minimum of one (1) product Id must be specified.")]
[MaxLength(50, ErrorMessage = "A maximum of twenty (20) product Ids can be specified.")]
public int[] ProductIdList { get; set; }
}
and the action method to handle the request has the following signature
[HttpGet]
[ValidateRequestModel]
public HttpResponseMessage ProductLookup([FromUri]ProductLookupRequest productLookupRequest)
The ProductLookupRequest object that the controller's action receives has ProductIdList populated with 3 entries each holding a value of 0.
Currently we handle zero value entries in the action method and output a HTTP 400 Bad Request if the array contains any zeroes.
ValidateRequestModelAttribute inspects the model state and if errors are found returns a HTTP 400 Bad Request without entering the action method. I'd like to keep this approach for querystring parameters sent with no value so that we don't have additional validation being performed in the controller's action methods.
QUESTION: Is there some built-in way of handling this with model binding or via JSON.NET? Or do I need to author a custom data annotation to handle this; one that inspects querystring parameters and if any are empty adds an error to the ModelState to invalidate it?