UriBuilder.Query Property
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets or sets any query information included in the URI, including the leading '?' character if not empty.
public:
property System::String ^ Query { System::String ^ get(); void set(System::String ^ value); };
public string Query { get; set; }
member this.Query : string with get, set
Public Property Query As String
The query information included in the URI.
The following example sets the Query property.
UriBuilder^ baseUri = gcnew UriBuilder
("http://www.contoso.com/default.aspx?Param1=7890");
String^ queryToAppend = "param2=1234";
if (baseUri->Query != nullptr && 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;
}
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;
open System
let baseUri = UriBuilder "http://www.contoso.com/default.aspx?Param1=7890"
let queryToAppend = "param2=1234"
baseUri.Query <-
if baseUri.Query <> null && baseUri.Query.Length > 1 then
// Note: In .NET Core and .NET 5+, you can simplify by removing
// the call to Substring(), which removes the leading "?" character.
baseUri.Query.Substring 1 + "&" + queryToAppend
else
queryToAppend
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.
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 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: