UriTemplate 类 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
一个表示统一资源标识符 (URI) 模板的类。
public ref class UriTemplatepublic class UriTemplatetype UriTemplate = classPublic 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”。 你可以调用 Match(Uri, Uri) 来测试给定的候选 URI 是否与给定的 URI 模板匹配。 也可以调用 UriTemplate 或 Uri 来使用 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) | 尝试将 Uri 与 UriTemplate 匹配。 | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| ToString() | 返回 UriTemplate 实例的字符串表示形式。 |