UriTemplateTable 类


一个表示一组关联 UriTemplate 对象的类。A class that represents an associative set of UriTemplate objects.

public ref class UriTemplateTable
public class UriTemplateTable
type UriTemplateTable = class
Public Class UriTemplateTable


下面的代码演示如何创建 UriTemplateTable,对其进行填充,并使用它与候选 Uri 进行匹配。The following code shows how to create a UriTemplateTable, populate it, and use it to match against a candidate Uri.

Uri prefix = new Uri("http://localhost/");

//Create a series of templates
UriTemplate weatherByCity  = new UriTemplate("weather/{state}/{city}");
UriTemplate weatherByCountry = new UriTemplate("weather/{country}/{village}");
UriTemplate weatherByState = new UriTemplate("weather/{state}");
UriTemplate traffic = new UriTemplate("traffic/*");
UriTemplate wildcard = new UriTemplate("*");

//Create a template table
UriTemplateTable table = new UriTemplateTable(prefix);
//Add each template to the table with some associated data
table.KeyValuePairs.Add(new KeyValuePair<UriTemplate, Object>(weatherByCity, "weatherByCity"));
table.KeyValuePairs.Add(new KeyValuePair<UriTemplate, Object>(weatherByCountry, "weatherByCountry"));
table.KeyValuePairs.Add(new KeyValuePair<UriTemplate, Object>(weatherByState, "weatherByState"));
table.KeyValuePairs.Add(new KeyValuePair<UriTemplate, Object>(traffic, "traffic"));

foreach (KeyValuePair<UriTemplate, Object> keyPair in table.KeyValuePairs)
    Console.WriteLine($"{keyPair.Key}, {keyPair.Value}");


//Call MatchSingle to retrieve some match results:
ICollection<UriTemplateMatch> results = null;
Uri weatherInSeattle = new Uri("http://localhost/weather/Washington/Seattle");

results = table.Match(weatherInSeattle);
if( results != null)
    Console.WriteLine("Matching templates:");
    foreach (UriTemplateMatch match in results)
Dim prefix As New Uri("http://localhost/")

' Create a series of templates
Dim weatherByCity As New UriTemplate("weather/ state}/ city}")
Dim weatherByCountry As New UriTemplate("weather/ country}/ village}")
Dim weatherByState As New UriTemplate("weather/ state}")
Dim traffic As New UriTemplate("traffic/*")
Dim wildcard As New UriTemplate("*")

' Create a template table
Dim table As New UriTemplateTable(prefix)
' Add each template to the table with some associated data
table.KeyValuePairs.Add(New KeyValuePair(Of UriTemplate, Object)(weatherByCity, "weatherByCity"))
table.KeyValuePairs.Add(New KeyValuePair(Of UriTemplate, Object)(weatherByCountry, "weatherByCountry"))
table.KeyValuePairs.Add(New KeyValuePair(Of UriTemplate, Object)(weatherByState, "weatherByState"))
table.KeyValuePairs.Add(New KeyValuePair(Of UriTemplate, Object)(traffic, "traffic"))

For Each keyPair As KeyValuePair(Of UriTemplate, Object) In table.KeyValuePairs
    Console.WriteLine("     0},  1}", keyPair.Key, keyPair.Value)


' Call MatchSingle to retrieve some match results:
Dim results As System.Collections.Generic.ICollection(Of UriTemplateMatch) = Nothing
Dim weatherInSeattle As Uri = New Uri("http://localhost/weather/Washington/Seattle")

results = table.Match(weatherInSeattle)
If results IsNot Nothing Then
    Console.WriteLine("Matching templates:")
    For Each match As UriTemplateMatch In results
        Console.WriteLine("    0}", match.Template)
End If


UriTemplateTable 是一组绑定到开发人员所选对象的关联 UriTemplate 对象。A UriTemplateTable is an associative set of UriTemplate objects bound to an object of the developer's choosing. 您可以通过它将候选统一资源标识符 (URI) 与集合中的模板进行匹配,然后检索与匹配的模板相关联的数据。It allows you to match candidate Uniform Resource Identifiers (URIs) against the templates in the set and retrieve the data associated with the matching templates. 在调用 UriTemplateTable 方法之前,可以更改 MakeReadOnly(Boolean) 的内容,调用时会发生下列其中一种类型的验证:The contents of UriTemplateTable can be changed until the MakeReadOnly(Boolean) method is called, at which time one of following types of validation occurs:

  • 如果在调用 MakeReadOnly(Boolean) 时传入 false,则 UriTemplateTable 会检查以确保表中没有多个结构等效的模板。When MakeReadOnly(Boolean) is called passing in false, the UriTemplateTable checks to make sure the table contains no multiple structurally-equivalent templates. 如果找到这样的模板,则会引发异常。If it finds such templates, it throws an exception. 如果想要确保只有一个模板与传入的 URI 匹配,则可将此验证类型与 MatchSingle(Uri) 结合使用。This type of validation is used in conjunction with MatchSingle(Uri) when you want to ensure only one template matches an incoming URI.

  • 如果在调用 MakeReadOnly(Boolean) 时传入 true,则 UriTemplateTable 中可能包含多个结构等效的模板。When MakeReadOnly(Boolean) is called passing in true, multiple structurally-equivalent templates can be contained within a UriTemplateTable. 但是,模板中的所有查询字符串必须无歧义;您可以使用相同的查询字符串。However, any query strings in the templates must not be ambiguous; identical query strings are allowed. 有关不明确查询字符串的详细信息,请参阅 UriTemplate 和 UriTemplateTableFor more information about ambiguous query strings, see UriTemplate and UriTemplateTable.



初始化 UriTemplateTable 类的新实例。Initializes a new instance of the UriTemplateTable class.


使用指定的键/值对集合初始化 UriTemplateTable 类的新实例。Initializes a new instance of the UriTemplateTable class with the specified collection of key/value pairs.


使用指定的基址初始化 UriTemplateTable 类的新实例。Initializes a new instance of the UriTemplateTable class with the specified base address.

UriTemplateTable(Uri, IEnumerable<KeyValuePair<UriTemplate,Object>>)

使用指定的基址和键/值对集合初始化 UriTemplateTable 类的新实例。Initializes a new instance of the UriTemplateTable class with the specified base address and collection of key/value pairs.



获取或设置 UriTemplateTable 实例的基址。Gets or sets the base address for the UriTemplateTable instance.


获取一个值,该值指定 UriTemplateTable 是否为只读。Gets a value that specifies whether the UriTemplateTable is read only.


获取由 UriTemplate 对象及其关联数据构成的键/值对集合。Gets a collection of key/value pairs that consist of UriTemplate objects and their associated data.


获取原始基址。Gets the original base address.



确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)

UriTemplateTable 设置为只读。Makes the UriTemplateTable read only.


尝试将候选 UriUriTemplateTable 匹配。Attempts to match a candidate Uri to the UriTemplateTable.


尝试将候选 UriUriTemplateTable 匹配。Attempts to match a candidate Uri to the UriTemplateTable.


创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)