UriBuilder.Query 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定 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 = 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 框架中,問號總是在字串前加上,即使字串本身已經以問號開頭。
- 在 .NET 5(以及 .NET Core)及之後版本中,如果字串本身沒有,則會在字串前加上問號。
查詢資訊依據 RFC 2396 進行逃逸。
Note
要在 .NET Framework 中為現有查詢資訊附加值,必須先移除前導問號,才能設定新值的屬性。 這是因為 .NET Framework 在設定屬性時總是在問號前加上。 .NET 5(及 .NET Core)及後續版本容忍導引問號,僅在必要時才會加導問號。