UriBuilder.Query 屬性

定義

取得或設定 URI 中包含的任何查詢資訊,如果不是空的,則包含前置 '?' 字元。

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

屬性值

URI 所包含的查詢資訊。

範例

下列範例會 Query 設定 屬性。

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

備註

屬性 Query 包含 URI 中包含的任何查詢資訊。 查詢資訊會以問號 (?) 分隔路徑資訊,並繼續到 URI 的結尾。 傳回的查詢資訊包含前置問號。 設定 Query 屬性時:

  • 在.NET Framework中,問號一律會前面加上字串,即使字串已經以問號開頭也一樣。
  • 在 .NET 5 (和 .NET Core) 和更新版本中,如果尚未存在,則會在字串前面加上問號。

查詢資訊會根據 RFC 2396 逸出。

注意

若要將值附加至.NET Framework中的常設查詢資訊,您必須先移除前置問號,才能使用新的值設定 屬性。 這是因為設定 屬性時,.NET Framework一律會加上問號。 .NET 5 (和 .NET Core) 和更新版本都能夠容忍前置問號,而且只會在必要時加上一個。

適用於

另請參閱