question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Could not bind an array of query param values with Request.Query.As<int[]>("Id")

See original GitHub issue
public 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:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Deilancommented, Apr 27, 2021

I cannot provide any reference to a language- or platform-agnostic spec on that topic. But since:

  • Carter is built upon ASP.NET Core infrastructure, and aforementioned way of working with query strings is the default in ASP.NET (Core)
  • main target audience of Carter is ASP.NET (Core) developers (in past or present)

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

0reactions
jchannoncommented, Nov 12, 2021

To get an array you can do var ids = ctx.Request.Query.AsMultiple<int>("id");

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found