-1

Using Asp.Net Core 2.2 I am calling an API using:

products?productsIds=1,2&offset=10&limit=4

But I get the error on model binding for productsIds:

The value '1,2' is not valid.

I tried with productsIds=1 and in that case it works.

And Offset and Limit also get the correct values ...

The API model and action are:

public class Request {
  public IList<Int32> ProductsIds { get; set; }
  public Int32 Limit { get; set; }
  public Int32 Offset { get; set; }
}

[ApiController]
public class ProductController : ControllerBase {

  [HttpGet("products")]
  public async Task<IActionResult> Get([FromQuery]Request request) {
    // Action code
  }

}

What am I missing?

Update

This is not similar to this Question as the solution proposed in that question uses [FromUri].

The attribute [FromUri] is not available in ASP.NET Core 2.2 ApiController (Microsoft Docs).

Tony Ngo
  • 19,166
  • 4
  • 38
  • 60
Miguel Moura
  • 36,732
  • 85
  • 259
  • 481

2 Answers2

1

Try this you can use HttpContext.Request.Query to get a params

like this

HttpContext.Request.Query["ProductsIds"]
Tony Ngo
  • 19,166
  • 4
  • 38
  • 60
  • But I do not wan to use HttpContext.Request.Query["ProductsIds"] ... I want the binding to be done automatically and I don't understand why ASP.NET core don't bind a simple List of Ints. – Miguel Moura May 17 '19 at 10:15
  • I understand but as I know the FromQuery can't get the array so you have to use this way – Tony Ngo May 17 '19 at 10:15
1

If you want to pass List as GET param use the following query

products?productsIds=1&productsIds=2
Bohdan Stupak
  • 1,455
  • 8
  • 15