I have a .NET Core 6.0 Azure Function HTTP Trigger.

I would it like to handle an array query string parameter in both of these common forms:

  • ?param=foo&param=escape%2Ctest
  • ?param=foo,escape%2Ctest

In both cases I would like to extract the array ["foo", "escape,test"].

I was surprised to find that the query string parse options available to me either did not support the comma syntax or URL decoded the query string as part of grouping the parameters.

I've tried using several different dotnet APIs:

Here's a demo in .NET Fiddle:

var qs = "?param=foo,escape%2Ctest";

var a = httpRequest.Query["param"];
// ["foo,escape,test"]

var b = HttpUtility.ParseQueryString(qs)["param"];
// "foo,escape,test"

var c = QueryHelpers.ParseQuery(qs)["param"];
// ["foo,escape,test"]

I am a little surprised that there is no support for the comma separated format. And I am very surprised that there does not seem to be a way to get the segmented query string without also decoding the query string parameters (which prevents distinguishing between param=foo,escape%2Ctest and ?param=foo,escape,test).

I will post my current solution as an "answer" below, but I am hoping that I am missing a simpler solution that uses a .NET library. If anyone can point out what I've missed here, I would appreciate it.

Unfortunately, there is no official specification in RFC 3986 > Section 3.4 detailing how to pass multiple values for a query parameter.

That said, browsers do seem to natively implement the multi-key approach when building a query string on their own. For example, in the following demo:

<form method="GET">
  <label for="cars">Choose a car:</label>
  <select name="cars" id="cars" multiple>
    <option value="audi" selected>Audi</option>
    <option value="saab" selected>Saab</option>
  </select>
  <button type="submit">Submit</button>
</form>

The get request will be sent with ?cars=audi&cars=saab

In the absence of a spec, with dotnet already supporting the multi-key approach with your desired result, and since browsers use multi-key natively, it may be worth updating the way you build query strings on the client.

Perhaps someone else can add a some clever way to parse in .net, but otherwise you may be fighting uphill against the implementation in dotnet.

Further Reading: