UriTemplate 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
一個代表統一資源識別碼(URI)範本的類別。
public ref class UriTemplate
public class UriTemplate
type UriTemplate = class
Public Class UriTemplate
- 繼承
-
UriTemplate
範例
以下程式碼示範如何建立 UriTemplate 實例,並將其綁定並匹配到候選 URI。
UriTemplate template = new UriTemplate("weather/{state}/{city}?forecast={day}");
Uri prefix = new Uri("http://localhost");
Console.WriteLine("PathSegmentVariableNames:");
foreach (string name in template.PathSegmentVariableNames)
{
Console.WriteLine(" {0}", name);
}
Console.WriteLine();
Console.WriteLine("QueryValueVariableNames:");
foreach (string name in template.QueryValueVariableNames)
{
Console.WriteLine(" {0}", name);
}
Console.WriteLine();
Uri positionalUri = template.BindByPosition(prefix, "Washington", "Redmond", "Today");
NameValueCollection parameters = new NameValueCollection();
parameters.Add("state", "Washington");
parameters.Add("city", "Redmond");
parameters.Add("day", "Today");
Uri namedUri = template.BindByName(prefix, parameters);
Uri fullUri = new Uri("http://localhost/weather/Washington/Redmond?forecast=today");
UriTemplateMatch results = template.Match(prefix, fullUri);
Console.WriteLine("Matching {0} to {1}", template.ToString(), fullUri.ToString());
if (results != null)
{
foreach (string variableName in results.BoundVariables.Keys)
{
Console.WriteLine(" {0}: {1}", variableName, results.BoundVariables[variableName]);
}
}
Dim template As UriTemplate = New UriTemplate("weather/{state}/{city}?forecast={day}")
Dim prefix As Uri = New Uri("http://localhost")
Console.WriteLine("PathSegmentVariableNames:")
For Each name As String In template.PathSegmentVariableNames
Console.WriteLine(" {0}", name)
Next
Console.WriteLine()
Console.WriteLine("QueryValueVariableNames:")
For Each name As String In template.QueryValueVariableNames
Console.WriteLine(" {0}", name)
Next
Console.WriteLine()
Dim positionalUri As Uri = template.BindByPosition(prefix, "Washington", "Redmond", "Today")
Dim parameters As NameValueCollection = New NameValueCollection()
parameters.Add("state", "Washington")
parameters.Add("city", "Redmond")
parameters.Add("day", "Today")
Dim namedUri As Uri = template.BindByName(prefix, parameters)
Dim fullUri As Uri = New Uri("http://localhost/weather/Washington/Redmond?forecast=today")
Dim results As UriTemplateMatch = template.Match(prefix, fullUri)
Console.WriteLine("Matching {0} to {1}", template.ToString(), fullUri.ToString())
If results IsNot Nothing Then
For Each variableName As String In results.BoundVariables.Keys
Console.WriteLine(" {0}: {1}", variableName, results.BoundVariables(variableName))
Next
End If
備註
URI 範本允許你定義一組結構相似的 URI。 範本是由兩個部分組成,一個路徑和一個查詢。 路徑是由一系列以斜線 (/) 分隔的區段所組成。 每個區段都可以有常值、變數值(以大括弧 [{ }] 撰寫,限制為符合一個區段的內容),或通配符(寫入為星號 [*],符合路徑的其餘部分),必須出現在路徑結尾。 可以完全省略查詢表達式。 如果存在,它會指定未排序的名稱/值組數列。 查詢表達式的元素可以是文字對(?x=2)或變數對(?x={val})。 不允許未配對的值。 以下範例展示了有效的範本字串:
「天氣/華盛頓/西雅圖」
「天氣/{州}/{城市}」
「天氣/*」
「天氣/{state}/{city}?forecast=today
“weather/{state}/{city}?forecast={day}
前述的 URI 範本可用於組織天氣報告。 用捲括括號包圍的段是變數,其他都是字面值。 你可以 UriTemplate 透過替換變數成實際值來轉換實 Uri 例。 例如,取模板「weather/{state}/{city}」,並輸入變數「{state}」和「{city}」的值,得到「weather/WA/Seattle」。 給定候選 URI,你可以透過呼叫 Match(Uri, Uri)來測試它是否符合該 URI 範本。 你也可以用UriTemplate實例從一組變數值Uri中呼叫 或 BindByName(Uri, NameValueCollection)來建立 。BindByPosition(Uri, String[])
建構函式
| 名稱 | Description |
|---|---|
| UriTemplate(String, Boolean, IDictionary<String,String>) |
初始化 UriTemplate 類別的新執行個體。 |
| UriTemplate(String, Boolean) |
初始化 UriTemplate 類別的新執行個體。 |
| UriTemplate(String, IDictionary<String,String>) |
初始化 UriTemplate 類別的新執行個體。 |
| UriTemplate(String) |
初始化一個新的類別實例 UriTemplate ,使用指定的範本字串。 |
屬性
| 名稱 | Description |
|---|---|
| Defaults |
會取得一組預設參數值的名稱/值對。 |
| IgnoreTrailingSlash |
規定在匹配候選 URI 時,模板中尾部斜線「/」是否應忽略。 |
| PathSegmentVariableNames |
會取得模板中路徑段中使用的變數名稱集合。 |
| QueryValueVariableNames |
取得範本查詢字串中使用的變數名稱集合。 |
方法
| 名稱 | Description |
|---|---|
| BindByName(Uri, IDictionary<String,String>, Boolean) |
從範本和參數集合建立一個新的 URI。 |
| BindByName(Uri, IDictionary<String,String>) |
從範本和參數集合建立一個新的 URI。 |
| BindByName(Uri, NameValueCollection, Boolean) |
從範本和參數集合建立一個新的 URI。 |
| BindByName(Uri, NameValueCollection) |
從範本和參數集合建立一個新的 URI。 |
| BindByPosition(Uri, String[]) |
從範本建立一個新的 URI 和一個參數值陣列。 |
| Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
| GetHashCode() |
做為預設哈希函式。 (繼承來源 Object) |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| IsEquivalentTo(UriTemplate) |
表示 a UriTemplate 在結構上是否等同於另一個。 |
| Match(Uri, Uri) |
嘗試將 a Uri 與 UriTemplate。 |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| ToString() |
回傳實例的 UriTemplate 字串表示。 |