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¶m=escape,test
?param=foo,escape,test
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:
System.Web.HttpUtility.ParseQueryString
Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery
Microsoft.AspNetCore.Http.HttpRequest.Query
Here's a demo in .NET Fiddle:
var qs = "?param=foo,escape,test";
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,test
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.
CodePudding user response:
Here is my current solution. Again, I'm hoping there is a less manual way to achieve this behavior.
public static IDictionary<string, List<string>> GroupByKey(this QueryString queryString)
{
if (!queryString.Contains("?"))
{
throw new ArgumentException("Expected query string to contain '?' character.");
}
var encodedUrl = queryString.Value;
var result = new Dictionary<string, List<string>>();
string trimmedQueryString = encodedUrl.Split('?')[1];
string[] queryStringSections = trimmedQueryString.Split('&');
foreach (var section in queryStringSections)
{
var sides = section.Split('=');
var key = sides[0];
var value = sides[1];
var values = value.Split(',');
var decodedValues = values.Select(x => WebUtility.UrlDecode(x));
if (!result.ContainsKey(key))
{
result.Add(key, new List<string>());
}
result[key].AddRange(decodedValues);
}
return result;
}
CodePudding user response:
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.