UriBuilder.Query Property

Definition

Gets or sets any query information included in the URI, including the leading '?' character if not empty.

public string Query { get; set; }

Property Value

The query information included in the URI.

Examples

The following example sets the Query property.

UriBuilder baseUri = new UriBuilder("http://www.contoso.com/default.aspx?Param1=7890");
string queryToAppend = "param2=1234";

if (baseUri.Query != null && baseUri.Query.Length > 1)
    // Note: In .NET Core and .NET 5+, you can simplify by removing
    // the call to Substring(), which removes the leading "?" character.
    baseUri.Query = baseUri.Query.Substring(1) + "&" + queryToAppend; 
else
    baseUri.Query = queryToAppend;

Remarks

The Query property contains any query information included in the URI. Query information is separated from the path information by a question mark (?) and continues to the end of the URI. The query information that's returned includes the leading question mark. When setting the Query property:

  • In .NET Framework, a question mark is always prepended to the string, even if the string already starts with a question mark.
  • In .NET 5 (and .NET Core) and later versions, a question mark is prepended to the string if it's not already present.

The query information is escaped according to RFC 2396.

Note

To append a value to existing query information in .NET Framework, you must remove the leading question mark before setting the property with the new value. This is because .NET Framework always prepends the question mark when setting the property. .NET 5 (and .NET Core) and later versions are tolerant to a leading question mark, and will only prepend one if necessary.

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

See also