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 模板匹配。 还可以通过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)

指示 a UriTemplate 在结构上是否等效于另一个结构。

Match(Uri, Uri)

尝试将 a 与 a UriUriTemplate匹配。

MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
ToString()

返回实例的 UriTemplate 字符串表示形式。

适用于