System.UriTemplate 类
方法描述
一个表示统一资源标识符 (URI) 模板的类。
语法定义(C# System.UriTemplate 类 的用法)
public class UriTemplate
构造函数
| 构造函数名称 | 构造函数描述 | 
|---|---|
| UriTemplate(String) | 使用指定的模板字符串初始化 UriTemplate 类的新实例。 | 
| UriTemplate(String, Boolean) | 初始化 UriTemplate 类的新实例。 | 
| UriTemplate(String, IDictionary | 初始化 UriTemplate 类的新实例。 | 
| UriTemplate(String, Boolean, IDictionary | 初始化 UriTemplate 类的新实例。 | 
成员/方法
| 方法名称 | 方法描述 | 
|---|---|
| BindByName(Uri, IDictionary | 利用模板和参数集合创建一个新的 URI。 | 
| BindByName(Uri, NameValueCollection) | 利用模板和参数集合创建一个新的 URI。 | 
| BindByName(Uri, IDictionary | 利用模板和参数集合创建一个新的 URI。 | 
| BindByName(Uri, NameValueCollection, Boolean) | 利用模板和参数集合创建一个新的 URI。 | 
| BindByPosition | 利用模板和参数值数组创建一个新的 URI。 | 
| Equals(Object) | 确定指定的 Object 是否等于当前的 Object。 (继承自 Object。) | 
| Finalize | 允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。) | 
| GetHashCode | 用作特定类型的哈希函数。 (继承自 Object。) | 
| GetType | 获取当前实例的 Type。 (继承自 Object。) | 
| IsEquivalentTo | 指示 UriTemplate 与其他模板的结构是否等效。 | 
| Match | 尝试将 URI 与 UriTemplate 匹配。 | 
| MemberwiseClone | 创建当前 Object 的浅表副本。 (继承自 Object。) | 
| ToString | 返回 UriTemplate 实例的字符串表示形式。 (重写 Object.ToString()。) | 
提示和注释
使用 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 模板匹配。也可以调用 BindByName(Uri, NameValueCollection) 或 BindByPosition(Uri, String[]) 来使用 UriTemplate 实例根据一组变量值创建 Uri。
System.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]);
    }
}
线程安全
此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。
版本信息
.NET Framework 受以下版本支持:4、3.5 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1
适用平台
Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。