Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

QueryString.Create(...) excludes key when StringValues is empty #969

Closed
austindrenski opened this issue Nov 16, 2017 · 2 comments
Closed

Comments

@austindrenski
Copy link

QueryString.Create(IEnumerable<KeyValuePair<string, StringValues>> parameters) doesn't append a key unless it has associated values.

This was unexpected because it means that parsing an incoming query string into an IEnumerable<KeyValuePair<string, StringValues>> and then immediately reconstructing a QueryString results in information loss.

We ran across this after some new middleware attempted to validate parameter values (by treating all query string entries as KeyValuePair<string, StringValues>) and unexpectedly removed a key meant to be read as a switch/flag/indicator in the event no values are supplied.

Is this behavior intentional or defined by a standard?

/// <summary>
/// Creates a query string composed from the given name value pairs.
/// </summary>
/// <param name="parameters"></param>
/// <returns>The resulting QueryString</returns>
public static QueryString Create(IEnumerable<KeyValuePair<string, StringValues>> parameters)
{
var builder = new StringBuilder();
bool first = true;
foreach (var pair in parameters)
{
foreach (var value in pair.Value)
{
builder.Append(first ? "?" : "&");
first = false;
builder.Append(UrlEncoder.Default.Encode(pair.Key));
builder.Append("=");
builder.Append(UrlEncoder.Default.Encode(value));
}
}
return new QueryString(builder.ToString());
}

@jkotalik
Copy link
Contributor

By the RFC 3986, QueryStrings don't need to have key=value data. So it does seem like a bug, however it is odd that you are using a special flag in the query string. Maybe as a work around, you can just check if the QueryString is empty rather than relying on a special value?

@aspnet-hello
Copy link

This issue was moved to dotnet/aspnetcore#2679

@aspnet aspnet locked and limited conversation to collaborators Jan 2, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants