Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

260
Visualizações
Parse query string arrays using either multi-key or comma syntax

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:

  • 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%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.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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;
}
over 4 years ago · Santiago Trujillo Relatório

0

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:

  • Correct way to pass multiple values for same parameter name in GET request
  • How to pass multiple parameters in a querystring
  • Authoritative position of duplicate HTTP GET query keys
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda