2

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?

1
  • you can work around this using route params config or implementing a custom model binder. Commented Mar 6, 2014 at 19:11

1 Answer 1

1

You can write your custom validation attribute for that and decorate the ProductIdList with it:

public class NoNullsAttribute : ValidationAttribute
{
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
       int[] values = value as int[];
       if (values != null)
       {
          foreach (int val in values)
          {
             if (val == 0)
             {
                return new ValidationResult("zero detected");
             }
          }
        }

        return ValidationResult.Success;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

This worked out for me; I just wasn't sure if there was a baked-in way of doing it or if I had to roll-my-own. Thanks for the code sample as well (+1)
@emgee checking a list of 3 items for 0 is a pretty custom task, so I guess there is nothing more simple than my sample code ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.