Home >Backend Development >C++ >How to Pass an Integer Array to an ASP.NET Web API Action Method?

How to Pass an Integer Array to an ASP.NET Web API Action Method?

Linda Hamilton
Linda HamiltonOriginal
2025-01-27 21:41:10767browse

How to Pass an Integer Array to an ASP.NET Web API Action Method?

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:

<code class="language-csharp">public IEnumerable<Category> GetCategories([FromUri] int[] categoryIds)
{
    // Database retrieval logic for categories
}</code>

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn