How to send integer array to ASP.NET Web API controller

I’m working with ASP.NET Web API version 4 and I need to send multiple integer values to my controller method. I want to pass an array of numbers but I’m not sure about the correct way to do it.

My controller method looks like this:

public IEnumerable<Product> GetProducts(int[] productIds){
    // logic to fetch products from repository
}

I tried calling it with this URL format:

/Products?productids=5,6,7,8

But it doesn’t seem to work properly. What’s the right approach to pass an array of integers through the query string to a Web API endpoint? Should I format the URL differently or modify my controller method?

just use post. get requests with big arrays look ugly and hit url length limits fast. your controller method’s fine - just add [HttpPost] and [FromBody] attributes. way cleaner than repeating parameter names or building custom binders.

Had this exact problem last month and went a completely different route. Skip the query string arrays - I built a custom model binder that handles comma-separated values automatically.

public class CommaDelimitedArrayModelBinder : DefaultModelBinder
{
    public override bool BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        if (bindingContext.ModelType == typeof(int[]))
        {
            var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
            if (value?.AttemptedValue != null)
            {
                var result = value.AttemptedValue.Split(',')
                    .Select(int.Parse).ToArray();
                bindingContext.Result = ModelBindingResult.Successful(result);
                return true;
            }
        }
        return base.BindModel(controllerContext, bindingContext);
    }
}

Register it in WebApiConfig and your original URL format works perfectly. Way more intuitive than repeating parameter names, and keeps URLs clean when you’re passing tons of IDs.

Comma separated won’t work - model binding doesn’t handle it that way by default.

You need to repeat the parameter name for each value:

/Products?productIds=5&productIds=6&productIds=7&productIds=8

I’ve hit this exact issue multiple times. The Web API model binder expects each array element to have the same parameter name.

You can also use array indexing:

/Products?productIds[0]=5&productIds[1]=6&productIds[2]=7&productIds[3]=8

Both will bind properly to your int parameter.

If you’re calling from JavaScript, build the query string like this:

const ids = [5, 6, 7, 8];
const queryString = ids.map(id => `productIds=${id}`).join('&');

Keep your controller method as is. It’s just the URL format that’s wrong, not your code.

Been there so many times. URL parameters work but get messy with hundreds of IDs.

I switched to POST requests for arrays. Much cleaner:

[HttpPost]
[Route("Products/ByIds")]
public IEnumerable<Product> GetProducts([FromBody] int[] productIds){
    // your logic
}

Send a simple JSON array:

[5, 6, 7, 8]

Now I just automate it. Built a Latenode workflow that handles array parameters automatically. Takes any format - comma separated, JSON arrays, whatever - and converts it for your API.

It watches incoming requests, parses parameters, transforms them, and forwards to your controller. No URL length limits or encoding headaches.

Saved me hours debugging parameter binding across projects.