Home > Software engineering >  .Net 6 WebApi passing array of int as parameter
.Net 6 WebApi passing array of int as parameter

Time:09-30

I'm trying to pass an array of int to the Controller but I'm getting empty array every time. I know I have to use [FromQuery]. What I'm doing wrong?

        [HttpGet("top10/{lineId}/{dateTo}/{groupingType}/{lathes}")]
    public async Task<IActionResult> GetTop10(byte lineId, DateTimeOffset dateTo, string groupingType, [FromQuery] int[] lathes)
    {
        // some stuff
    }

And my URL looks like that:

https://localhost:44439/api/v1/dashboards/overview/top10/1/2022-09-28/days/lathes=2&lathes=3&lathes=4&lathes=6

CodePudding user response:

The FromQuery attribute "Specifies that a parameter or property should be bound using the request query string." docs

So the issue is that you are including the lathes in the route but asking the framework to read it from the query string.

Specify the endpoint route as this:

[HttpGet("top10/{lineId}/{dateTo}/{groupingType}")]
public async Task<IActionResult> GetTop10(byte lineId, DateTimeOffset dateTo, string groupingType, [FromQuery] int[] lathes)
{
    // some stuff
}

And add the lathes as query string ("?")

https://localhost:44439/api/v1/dashboards/overview/top10/1/2022-09-28/days?lathes=2&lathes=3&lathes=4&lathes=6
  • Related