Home >Backend Development >C++ >How to Pass an Integer Array to an ASP.NET Web API?
Sending Integer Arrays to ASP.NET Web APIs: A Practical Guide
This guide addresses the common challenge of correctly passing integer arrays to ASP.NET Web APIs. The problem often stems from incorrect parameter definition within the API and flawed URL construction.
The Solution:
Here's a step-by-step approach to resolve this issue:
Adjusting the API Method: The key is to use the [FromUri]
attribute when defining the array parameter in your Web API controller action:
<code class="language-csharp">public IEnumerable<category> GetCategories([FromUri] int[] categoryIds) { // Database retrieval logic for categories }</code>
Correct URL Formatting: The URL must be structured with repeated query parameters, one for each integer in the array. Example:
<code>/Categories?categoryids=1&categoryids=2&categoryids=3</code>
Making the API Call: Submitting this correctly formatted URL to your Web API will successfully transmit the integer array to the GetCategories
method.
By implementing these changes, your ASP.NET Web API will accurately receive and process the integer array as expected.
The above is the detailed content of How to Pass an Integer Array to an ASP.NET Web API?. For more information, please follow other related articles on the PHP Chinese website!