Could not bind an array of query param values with Request.Query.As<int[]>("Id")
See original GitHub issuepublic sealed class AppCarterModule : CarterModule
{
private static readonly Actor[] Actors = new[]
{
new Actor() { Id = 1, Name = "First"},
new Actor() { Id = 2, Name = "Second"},
new Actor() { Id = 3, Name = "Third"},
};
public AppCarterModule()
{
Get("/actors", async ctx =>
{
IEnumerable<Actor> result = Actors;
// ids is always null for "/actors?Id=1&Id=3"
var ids = ctx.Request.Query.As<int[]>("Id");
if (ids != null)
{
result = result.Where(x => ids.Contains(x.Id));
}
await ctx.Response.Negotiate(result);
});
}
}
GET /actors?id=1&id=3
returns all 3 records instead of 2 because ctx.Request.Query.As<int[]>("Id")
returns null
instead of [1,3]
Repository with a failing test https://github.com/deilan-issues-samples/Carter.Issues
public async Task Test1()
{
var client = _factory.CreateClient();
var result = await client.GetAsync("/actors?Id=1&Id=3");
var str = await result.Content.ReadAsStringAsync();
var array = JsonConvert.DeserializeObject<dynamic[]>(str);
Assert.Equal(2, array.Length);
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
How to pass an array within a query string?
I want that query string value to be treated as an array- I don't want the array to be exploded so that it...
Read more >Arrays in query params
A client MAY request that an endpoint return only specific fields in the response on a per-type basis by including a fields[TYPE] parameter....
Read more >Passing Information via Query Strings
You can pass multiple values for the same field within a query string. The result will be a combined field in your response...
Read more >Parameter binding in Minimal API applications
Parameter binding is the process of converting request data into strongly typed parameters that are expressed by route handlers.
Read more >YQL Query Language Reference - Vespa Documentation
Work around using a "magic" value (like MAXINT) that is not normally used in the documents. Since Vespa 7.520.3, YQL queries do not...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I cannot provide any reference to a language- or platform-agnostic spec on that topic. But since:
I’d say it should be also the default way in Carter too. Other ways are also would be nice to have, but as an opt-in.
(Some of) ASP.NET Core utils’, used to work with query params:
In particular: https://github.com/dotnet/aspnetcore/blob/4bd244935a88384def10cce77e68bc2c79cba3a0/src/Http/Http.Extensions/test/QueryBuilderTests.cs#L74-L84
https://github.com/dotnet/aspnetcore/blob/4bd244935a88384def10cce77e68bc2c79cba3a0/src/Http/WebUtilities/test/QueryHelpersTests.cs#L32-L39
To get an array you can do
var ids = ctx.Request.Query.AsMultiple<int>("id");