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

Hemanshu Bhojak picture Hemanshu Bhojak · Apr 2, 2012 · Viewed 308.8k times · Source

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:

/Categories?categoryids=1,2,3,4

Answer

Lavel picture Lavel · Jun 19, 2012

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

GetCategories([FromUri] int[] categoryIds)

And send request:

/Categories?categoryids=1&categoryids=2&categoryids=3