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.
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.