'.Net 6 pass array in query string
I have an issue with passing string array in query string and I don't know what's happening. here is the request:
.../api/customers/todos?customerValue=0&branchId=120&allianceId=1&searchValue=&offset=0&count=14&statuses=one&statuses=two&statuses=three
and this is my method:
public async Task<PaginatedResult<TodoVm>> GetTodos(decimal? customerValue, string branchId,
long? allianceId, string searchValue,
string[] statuses = null,
int offset = 0, int count = 20)
{
//do some stuff and return response
}
when I call the api it gives me this error and cannot bind the statues parameter:
{
"title": "One or more validation errors occurred.",
"status": 400,
"errors": {
"$": [
"The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. Path: $ | LineNumber: 0 | BytePositionInLine: 0."
]
}
}
but when I use FromQuery it works! like this:
public async Task<PaginatedResult<TodoVm>> GetTodos(decimal? customerValue, string branchId,
long? allianceId, string searchValue,
[FromQuery(Name = "statuses")] string[] statuses = null,
int offset = 0, int count = 20)
{
//do some stuff and return response
}
other parameters will be mapped correctly but for array I have to use FromQuery.
FYI, this is how I add controller in Startup:
services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNameCaseInsensitive = false;
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
options.JsonSerializerOptions.AllowTrailingCommas = false;
});
UPDATE
I figured it out that in asp.net core Arrays are treated as body parameters in [ApiController] so for binding array values so you need to specify [FromQuery].
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
