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%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:
- <code><i>System.Web.</i><b>HttpUtility.ParseQueryString</b></code>
- <code><i>Microsoft.AspNetCore.WebUtilities.</i><b>QueryHelpers.ParseQuery</b></code>
- <code><i>Microsoft.AspNetCore.Http.</i><b>HttpRequest.Query</b></code>
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.