Skip to content

Commit fabcdef

Browse files
authored
Enhance ParseQuery method documentation and ensure case-insensitive dictionary for null input (#63747)
1 parent ecf3c2d commit fabcdef

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/Http/WebUtilities/src/QueryHelpers.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,18 @@ public static string AddQueryString(
117117
/// Parse a query string into its component key and value parts.
118118
/// </summary>
119119
/// <param name="queryString">The raw query string value, with or without the leading '?'.</param>
120-
/// <returns>A collection of parsed keys and values.</returns>
120+
/// <returns>
121+
/// A collection of parsed keys and values. The returned <see cref="Dictionary{TKey, TValue}"/> always uses a
122+
/// case-insensitive (<see cref="StringComparer.OrdinalIgnoreCase"/>) key comparer so keys can be accessed
123+
/// in a case-insensitive manner regardless of whether the input contained any elements.
124+
/// </returns>
121125
public static Dictionary<string, StringValues> ParseQuery(string? queryString)
122126
{
123127
var result = ParseNullableQuery(queryString);
124128

125129
if (result == null)
126130
{
127-
return new Dictionary<string, StringValues>();
131+
return new Dictionary<string, StringValues>(0, StringComparer.OrdinalIgnoreCase);
128132
}
129133

130134
return result;

src/Http/WebUtilities/test/QueryHelpersTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,12 @@ public void AddQueryStringWithEnumerableOfKeysAndStringValues(string uri, string
198198
var result = QueryHelpers.AddQueryString(uri, queryStrings);
199199
Assert.Equal(expectedUri, result);
200200
}
201+
202+
[Fact]
203+
public void ParseQueryNullInputReturnsCaseInsensitiveDictionary()
204+
{
205+
var collection = QueryHelpers.ParseQuery(null);
206+
collection["AbC"] = "value";
207+
Assert.True(collection.Remove("abc"));
208+
}
201209
}

0 commit comments

Comments
 (0)