Bewerken

Share via


UriBuilder.Query Property

Definition

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

Property Value

The query information included in the URI.

Examples

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

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

See also