UriTemplate 類別

定義

代表統一資源識別元 (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})。 不允許使用未成對的值。 下列範例將顯示有效的樣板字串:

  • "weather/WA/Seattle"

  • "weather/{state}/{city}"

  • "weather/*"

  • "weather/{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 樣板。 您也可以呼叫 UriTemplateUri,以便使用 BindByName(Uri, NameValueCollection) 執行個體,從一組變數值建立 BindByPosition(Uri, String[])

建構函式

UriTemplate(String)

使用指定的樣板字串,初始化 UriTemplate 類別的新執行個體。

UriTemplate(String, Boolean)

初始化 UriTemplate 類別的新執行個體。

UriTemplate(String, Boolean, IDictionary<String,String>)

初始化 UriTemplate 類別的新執行個體。

UriTemplate(String, IDictionary<String,String>)

初始化 UriTemplate 類別的新執行個體。

屬性

Defaults

取得任何預設參數值之名稱/值組的集合。

IgnoreTrailingSlash

指定比對候選 URI 時是否應忽略樣板中的結尾斜線 "/"。

PathSegmentVariableNames

取得在樣板中的路徑區段內使用之變數名稱的集合。

QueryValueVariableNames

取得在樣板中的查詢字串內使用之變數名稱的集合。

方法

BindByName(Uri, IDictionary<String,String>)

從範本和參數集合建立新的 URI。

BindByName(Uri, IDictionary<String,String>, Boolean)

從範本和參數集合建立新的 URI。

BindByName(Uri, NameValueCollection)

從範本和參數集合建立新的 URI。

BindByName(Uri, NameValueCollection, Boolean)

從範本和參數集合建立新的 URI。

BindByPosition(Uri, String[])

從此樣板和參數值的陣列建立新的 URI。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsEquivalentTo(UriTemplate)

指出 UriTemplate 在結構上是否與另一個樣板相等。

Match(Uri, Uri)

嘗試比對 UriUriTemplate

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回 UriTemplate 執行個體的字串表示。

適用於