UriTemplate クラス

定義

Uri (Uniform Resource Identifier) テンプレートを表すクラス。

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 のセットを定義できます。 テンプレートは、パスとクエリの 2 つの部分で構成されます。 パスは、スラッシュ (/) で区切られた一連のセグメントで構成されます。 各セグメントには、リテラル値、変数値 (1 つのセグメントの内容に一致するように制約された中かっこ [{ }] 内に書き込まれます)、またはワイルドカード ("残りのパス" と一致するアスタリスク [*] として書き込まれます)、パスの末尾に表示する必要があります。 クエリ式は完全に省略できます。 存在する場合は、順序付けられていない一連の名前と値のペアを指定します。 クエリ式の要素には、リテラル ペア (?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 テンプレートと一致するかどうかをテストできます。 UriTemplateインスタンスを使用して、UriまたはBindByName(Uri, NameValueCollection)を呼び出すことによって、一連の変数値からBindByPosition(Uri, String[])を作成することもできます。

コンストラクター

名前 説明
UriTemplate(String, Boolean, IDictionary<String,String>)

UriTemplate クラスの新しいインスタンスを初期化します。

UriTemplate(String, Boolean)

UriTemplate クラスの新しいインスタンスを初期化します。

UriTemplate(String, IDictionary<String,String>)

UriTemplate クラスの新しいインスタンスを初期化します。

UriTemplate(String)

指定したテンプレート文字列を使用して、 UriTemplate クラスの新しいインスタンスを初期化します。

プロパティ

名前 説明
Defaults

既定のパラメーター値の名前と値のペアのコレクションを取得します。

IgnoreTrailingSlash

候補 URI を照合するときに、テンプレートの末尾のスラッシュ "/" を無視するかどうかを指定します。

PathSegmentVariableNames

テンプレート内のパス セグメント内で使用される変数名のコレクションを取得します。

QueryValueVariableNames

テンプレート内のクエリ文字列内で使用される変数名のコレクションを取得します。

メソッド

名前 説明
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)

UriTemplateが他のと構造的に等しいかどうかを示します。

Match(Uri, Uri)

UriUriTemplateの照合を試みます。

MemberwiseClone()

現在の Objectの簡易コピーを作成します。

(継承元 Object)
ToString()

UriTemplate インスタンスの文字列形式を返します。

適用対象