Passing Integer Arrays to ASP.NET Web API Action Methods
This guide demonstrates how to effectively transmit integer arrays to your ASP.NET Web API 4.x action methods. The [FromUri]
attribute provides a straightforward solution for binding array data from the URL's query string.
To implement this, simply precede your action method parameter with the [FromUri]
attribute. Here's an example:
public IEnumerable<Category> GetCategories([FromUri] int[] categoryIds) { // Database retrieval logic for categories }
With the [FromUri]
attribute in place, you can send integer arrays via the URL's query string. For instance, to pass the integers 1, 2, and 3, use this URL format:
<code>/Categories?categoryIds=1&categoryIds=2&categoryIds=3</code>
The Web API will automatically populate the categoryIds
parameter with the provided array values. Your action method can then utilize this array to fetch the corresponding categories from your database.
The above is the detailed content of How to Pass an Integer Array to an ASP.NET Web API Action Method?. For more information, please follow other related articles on the PHP Chinese website!