QueryStringUtilities Class

Provides methods for parsing and manipulating query strings.

public static class QueryStringUtilities

Methods

NameDescription
AddOrReplaceQueryString(string, IEnumerable<KeyValuePair<string, StringValues>>)Adds or replaces the given query keys and values in the URI.
AddOrReplaceQueryString(string, IEnumerable<KeyValuePair<string, string?>>)Adds or replaces the given query keys and values in the URI.
AddOrReplaceQueryString(string, IEnumerable<(string Name, StringValues Value)>)Adds or replaces the given query keys and values in the URI.
AddOrReplaceQueryString(string, IEnumerable<(string Name, string? Value)>)Adds or replaces the given query keys and values in the URI.
AddOrReplaceQueryString(string, string, string?)Adds or replaces the given query key and value in the URI.
AddQueryString(string, IDictionary<string, string?>)Append the given query keys and values to the URI.
AddQueryString(string, IEnumerable<KeyValuePair<string, StringValues>>)Append the given query keys and values to the URI.
AddQueryString(string, IEnumerable<KeyValuePair<string, string?>>)Append the given query keys and values to the URI.
AddQueryString(string, IEnumerable<(string Name, StringValues Value)>)Append the given query keys and values to the URI.
AddQueryString(string, IEnumerable<(string Name, string? Value)>?)Append the given query keys and values to the URI.
AddQueryString(string, string, string?)Append the given query key and value to the URI.
ParseNullableQuery(string?)Parse a query string into its component key and value parts.
ParseQuery(string?)Parse a query string into its component key and value parts.
RemoveQueryString(string, string)Removes the specified query parameter from the URI.
SetQueryString(string, IEnumerable<KeyValuePair<string, StringValues>>)Replace the query string with the given query key and value.
SetQueryString(string, IEnumerable<KeyValuePair<string, string?>>)Replace the query string with the given query key and value.

AddOrReplaceQueryString overloads

AddOrReplaceQueryString(string, IEnumerable<KeyValuePair<string, StringValues>>)

QueryStringUtilities.cs

Adds or replaces the given query keys and values in the URI.

public static string AddOrReplaceQueryString(string uri, IEnumerable<KeyValuePair<string, StringValues>> queryString)
Parameters
uri string
The base URI.
queryString IEnumerable<KeyValuePair<string, StringValues>>
A collection of name value query pairs to set.
Returns

string The combined result.

Exceptions
ArgumentNullException
uri is null.
ArgumentNullException
queryString is null.

AddOrReplaceQueryString(string, IEnumerable<KeyValuePair<string, string?>>)

QueryStringUtilities.cs

Adds or replaces the given query keys and values in the URI.

public static string AddOrReplaceQueryString(string uri, IEnumerable<KeyValuePair<string, string?>> queryString)
Parameters
uri string
The base URI.
queryString IEnumerable<KeyValuePair<string, string?>>
A collection of name value query pairs to set.
Returns

string The combined result.

Exceptions
ArgumentNullException
uri is null.
ArgumentNullException
queryString is null.

AddOrReplaceQueryString(string, IEnumerable<(string Name, StringValues Value)>)

QueryStringUtilities.cs

Adds or replaces the given query keys and values in the URI.

public static string AddOrReplaceQueryString(string uri, IEnumerable<(string Name, StringValues Value)> queryString)
Parameters
uri string
The base URI.
queryString IEnumerable<(string Name, StringValues Value)>
A collection of name value query pairs to set.
Returns

string The combined result.

Exceptions
ArgumentNullException
uri is null.
ArgumentNullException
queryString is null.

AddOrReplaceQueryString(string, IEnumerable<(string Name, string? Value)>)

QueryStringUtilities.cs

Adds or replaces the given query keys and values in the URI.

public static string AddOrReplaceQueryString(string uri, IEnumerable<(string Name, string? Value)> queryString)
Parameters
uri string
The base URI.
queryString IEnumerable<(string Name, string? Value)>
A collection of name value query pairs to set.
Returns

string The combined result.

Exceptions
ArgumentNullException
uri is null.
ArgumentNullException
queryString is null.

AddOrReplaceQueryString(string, string, string?)

QueryStringUtilities.cs

Adds or replaces the given query key and value in the URI.

public static string AddOrReplaceQueryString(string uri, string name, string? value)
Parameters
uri string
The base URI.
name string
The name of the query key.
value string?
The query value.
Returns

string The combined result.

Exceptions
ArgumentNullException
uri is null.
ArgumentNullException
name is null.

AddQueryString overloads

AddQueryString(string, IDictionary<string, string?>)

QueryStringUtilities.cs

Append the given query keys and values to the URI.

public static string AddQueryString(string uri, IDictionary<string, string?> queryString)
Parameters
uri string
The base URI.
queryString IDictionary<string, string?>
A dictionary of query keys and values to append.
Returns

string The combined result.

Exceptions
ArgumentNullException
uri is null.
ArgumentNullException
queryString is null.

AddQueryString(string, IEnumerable<KeyValuePair<string, StringValues>>)

QueryStringUtilities.cs

Append the given query keys and values to the URI.

public static string AddQueryString(string uri, IEnumerable<KeyValuePair<string, StringValues>> queryString)
Parameters
uri string
The base URI.
queryString IEnumerable<KeyValuePair<string, StringValues>>
A collection of query names and values to append.
Returns

string The combined result.

Exceptions
ArgumentNullException
uri is null.
ArgumentNullException
queryString is null.

AddQueryString(string, IEnumerable<KeyValuePair<string, string?>>)

QueryStringUtilities.cs

Append the given query keys and values to the URI.

public static string AddQueryString(string uri, IEnumerable<KeyValuePair<string, string?>> queryString)
Parameters
uri string
The base URI.
queryString IEnumerable<KeyValuePair<string, string?>>
A collection of name value query pairs to append.
Returns

string The combined result.

Exceptions
ArgumentNullException
uri is null.
ArgumentNullException
queryString is null.

AddQueryString(string, IEnumerable<(string Name, StringValues Value)>)

QueryStringUtilities.cs

Append the given query keys and values to the URI.

public static string AddQueryString(string uri, IEnumerable<(string Name, StringValues Value)> queryString)
Parameters
uri string
The base URI.
queryString IEnumerable<(string Name, StringValues Value)>
A collection of query names and values to append.
Returns

string The combined result.

Exceptions
ArgumentNullException
uri is null.
ArgumentNullException
queryString is null.

AddQueryString(string, IEnumerable<(string Name, string? Value)>?)

QueryStringUtilities.cs

Append the given query keys and values to the URI.

public static string AddQueryString(string uri, IEnumerable<(string Name, string? Value)>? queryString)
Parameters
uri string
The base URI.
queryString IEnumerable<(string Name, string? Value)>?
A collection of query names and values to append, or null.
Returns

string The combined result.

Exceptions
ArgumentNullException
uri is null.
Remarks

Unlike the other overloads, a null queryString returns uri unchanged.

AddQueryString(string, string, string?)

QueryStringUtilities.cs

Append the given query key and value to the URI.

public static string AddQueryString(string uri, string name, string? value)
Parameters
uri string
The base URI.
name string
The name of the query key.
value string?
The query value.
Returns

string The combined result.

Exceptions
ArgumentNullException
uri is null.
ArgumentNullException
name is null.
Remarks

A null value appends nothing, which is how QueryStringUtilities.RemoveQueryString drops a parameter.

ParseNullableQuery(string?)

QueryStringUtilities.cs

Parse a query string into its component key and value parts.

public static QueryStringParameterCollection? ParseNullableQuery(string? queryString)

Parameters

queryString string?
The raw query string value, with or without the leading '?'.

Returns

QueryStringParameterCollection? A collection of parsed keys and values, null if there are no entries.

ParseQuery(string?)

QueryStringUtilities.cs

Parse a query string into its component key and value parts.

public static QueryStringParameterCollection ParseQuery(string? queryString)

Parameters

queryString string?
The raw query string value, with or without the leading '?'.

Returns

QueryStringParameterCollection A collection of parsed keys and values.

RemoveQueryString(string, string)

QueryStringUtilities.cs

Removes the specified query parameter from the URI.

public static string RemoveQueryString(string uri, string name)

Parameters

uri string
The base URI.
name string
The name of the query key.

Returns

string The combined result.

Exceptions

ArgumentNullException
uri is null.
ArgumentNullException
name is null.

SetQueryString overloads

SetQueryString(string, IEnumerable<KeyValuePair<string, StringValues>>)

QueryStringUtilities.cs

Replace the query string with the given query key and value.

public static string SetQueryString(string uri, IEnumerable<KeyValuePair<string, StringValues>> queryString)
Parameters
uri string
The base URI.
queryString IEnumerable<KeyValuePair<string, StringValues>>
A collection of name value query pairs to set.
Returns

string The combined result.

Exceptions
ArgumentNullException
uri is null.
ArgumentNullException
queryString is null.

SetQueryString(string, IEnumerable<KeyValuePair<string, string?>>)

QueryStringUtilities.cs

Replace the query string with the given query key and value.

public static string SetQueryString(string uri, IEnumerable<KeyValuePair<string, string?>> queryString)
Parameters
uri string
The base URI.
queryString IEnumerable<KeyValuePair<string, string?>>
A collection of name value query pairs to set.
Returns

string The combined result.

Exceptions
ArgumentNullException
uri is null.
ArgumentNullException
queryString is null.