Home >Backend Development >C++ >How to Pass an Array of Integers to an ASP.NET Web API Using [FromUri]?

How to Pass an Array of Integers to an ASP.NET Web API Using [FromUri]?

Patricia Arquette
Patricia ArquetteOriginal
2025-01-27 21:31:09481browse

How to Pass an Array of Integers to an ASP.NET Web API Using [FromUri]?

Using [FromUri] to Send Integer Arrays to ASP.NET Web APIs

In ASP.NET Web API development, you'll often need to send arrays of integers as parameters to your API methods. The [FromUri] attribute provides a straightforward way to achieve this.

Example Implementation

Let's say you have an API method designed to retrieve categories based on a list of IDs:

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

Request URL Structure

To pass the integer array, you'll construct the request URL using query parameters:

<code>/Categories?categoryIds=1&categoryIds=2&categoryIds=3</code>

Understanding [FromUri]

The [FromUri] attribute tells the Web API framework to expect the parameter's values from the URI's query string. It handles the binding of these values to the categoryIds array.

Request Example

The URL above would result in the GetCategories method receiving an integer array containing [1, 2, 3].

Important Considerations

When working with [FromUri] for arrays:

  • Multiple query parameters are used, each with the same name as the array parameter (e.g., categoryIds).
  • If no query parameters are provided, the array will be null.

This approach offers a clean and efficient method for passing integer arrays to your ASP.NET Web APIs.

The above is the detailed content of How to Pass an Array of Integers to an ASP.NET Web API Using [FromUri]?. 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