Home >Backend Development >C++ >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:
categoryIds
).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!