Pass an array of integers to ASP.NET Web API?


I have an ASP.NET Web API (version 4) REST service where I need to pass an array of integers.

Here is my action method:

public IEnumerable<Category> GetCategories(int[] categoryIds){
// code to retrieve categories from database

And this is the URL that I have tried:

1/9/2018 5:47:45 PM

Accepted Answer

You just need to add [FromUri] before parameter, looks like:

GetCategories([FromUri] int[] categoryIds)

And send request:

3/14/2016 7:47:44 AM

As Filip W points out, you might have to resort to a custom model binder like this (modified to bind to actual type of param):

public IEnumerable<Category> GetCategories([ModelBinder(typeof(CommaDelimitedArrayModelBinder))]long[] categoryIds) 
    // do your thing

public class CommaDelimitedArrayModelBinder : IModelBinder
    public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
        var key = bindingContext.ModelName;
        var val = bindingContext.ValueProvider.GetValue(key);
        if (val != null)
            var s = val.AttemptedValue;
            if (s != null)
                var elementType = bindingContext.ModelType.GetElementType();
                var converter = TypeDescriptor.GetConverter(elementType);
                var values = Array.ConvertAll(s.Split(new[] { ","},StringSplitOptions.RemoveEmptyEntries),
                    x => { return converter.ConvertFromString(x != null ? x.Trim() : x); });

                var typedValues = Array.CreateInstance(elementType, values.Length);

                values.CopyTo(typedValues, 0);

                bindingContext.Model = typedValues;
                // change this line to null if you prefer nulls to empty arrays 
                bindingContext.Model = Array.CreateInstance(bindingContext.ModelType.GetElementType(), 0);
            return true;
        return false;

And then you can say:

/Categories?categoryids=1,2,3,4 and ASP.NET Web API will correctly bind your categoryIds array.


I recently came across this requirement myself, and I decided to implement an ActionFilter to handle this.

public class ArrayInputAttribute : ActionFilterAttribute
    private readonly string _parameterName;

    public ArrayInputAttribute(string parameterName)
        _parameterName = parameterName;
        Separator = ',';

    public override void OnActionExecuting(HttpActionContext actionContext)
        if (actionContext.ActionArguments.ContainsKey(_parameterName))
            string parameters = string.Empty;
            if (actionContext.ControllerContext.RouteData.Values.ContainsKey(_parameterName))
                parameters = (string) actionContext.ControllerContext.RouteData.Values[_parameterName];
            else if (actionContext.ControllerContext.Request.RequestUri.ParseQueryString()[_parameterName] != null)
                parameters = actionContext.ControllerContext.Request.RequestUri.ParseQueryString()[_parameterName];

            actionContext.ActionArguments[_parameterName] = parameters.Split(Separator).Select(int.Parse).ToArray();

    public char Separator { get; set; }

I am applying it like so (note that I used 'id', not 'ids', as that is how it is specified in my route):

[ArrayInput("id", Separator = ';')]
public IEnumerable<Measure> Get(int[] id)
    return id.Select(i => GetData(i));

And the public url would be:


You may have to refactor this to meet your specific needs.


In case someone would need - to achieve same or similar thing(like delete) via POST instead of FromUri, use FromBody and on client side(JS/jQuery) format param as $.param({ '': categoryids }, true)


public IHttpActionResult Remove([FromBody] int[] categoryIds)


        type: 'POST',
        data: $.param({ '': categoryids }, true),
        url: url,

The thing with $.param({ '': categoryids }, true) is that it .net will expect post body to contain urlencoded value like =1&=2&=3 without parameter name, and without brackets.


Easy way to send array params to web api


public IEnumerable<Category> GetCategories([FromUri]int[] categoryIds){
 // code to retrieve categories from database

Jquery : send JSON object as request params

//success response

It will generate your request URL like ../api/categories/GetCategories?categoryIds=1&categoryIds=2&categoryIds=3&categoryIds=4


You may try this code for you to take comma separated values / an array of values to get back a JSON from webAPI

 public class CategoryController : ApiController
     public List<Category> Get(String categoryIDs)
         List<Category> categoryRepo = new List<Category>();

         String[] idRepo = categoryIDs.Split(',');

         foreach (var id in idRepo)
             categoryRepo.Add(new Category()
                 CategoryID = id,
                 CategoryName = String.Format("Category_{0}", id)
         return categoryRepo;

 public class Category
     public String CategoryID { get; set; }
     public String CategoryName { get; set; }

Output :


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]