UriTemplate UriTemplate UriTemplate UriTemplate Class

定义

一个表示统一资源标识符 (URI) 模板的类。A class that represents a Uniform Resource Identifier (URI) template.

public ref class UriTemplate
public class UriTemplate
type UriTemplate = class
Public Class UriTemplate
继承
UriTemplateUriTemplateUriTemplateUriTemplate

示例

下面的代码演示如何创建 UriTemplate 实例,并将其绑定和匹配到候选 URI。The following code demonstrates how to create a UriTemplate instance, and bind and match it to a candidate 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。A URI template allows you to define a set of structurally similar URIs. 模板由两部分组成,即路径和查询。Templates are composed of two parts, a path and a query. 路径由一系列由斜杠 (/) 分隔的段组成。A path consists of a series of segments delimited by a slash (/). 每个段都可以具有文本值、变量值(书写在大括号 [{ }] 内,且被限制为仅与一个段的内容匹配)或必须出现在路径末端的通配符(书写为星号 [*],与“路径的其余部分”匹配)。Each segment can have a literal value, a variable value (written within curly braces [{ }], constrained to match the contents of exactly one segment), or a wildcard (written as an asterisk [*], which matches "the rest of the path"), which must appear at the end of the path. 查询表达式可以完全省略。The query expression can be omitted entirely. 如果出现表达式,则它指定一组无序的名称/值对。If present, it specifies an unordered series of name/value pairs. 查询表达式的元素可以是文本对 (?x=2),也可以是变量对 (?x={val})。Elements of the query expression can be either literal pairs (?x=2) or variable pairs (?x={val}). 不允许使用不成对的值。Unpaired values are not permitted. 下面的示例演示有效的模板字符串:The following examples show valid template strings:

  • "weather/WA/Seattle""weather/WA/Seattle"

  • "weather/{state}/{city}""weather/{state}/{city}"

  • "weather/*""weather/*"

  • "weather/{state}/{city}?forecast=today"weather/{state}/{city}?forecast=today

  • "weather/{state}/{city}?forecast={day}"weather/{state}/{city}?forecast={day}

前面的 URI 模板可用于组织天气预报。The preceding URI templates might be used for organizing weather reports. 括在大括号中的段都是变量,其他都是文本。Segments enclosed in curly braces are variables, everything else is a literal. 您可以使用实际的值替换变量,将 UriTemplate 实例转换成 UriYou can convert a UriTemplate instance into a Uri by replacing variables with actual values. 例如,使用模板“weather/{state}/{city}”,在变量“{state}”和“{city}”中输入值,便会得到“weather/WA/Seattle”。For example, taking the template "weather/{state}/{city}" and putting in values for the variables "{state}" and "{city}" gives you "weather/WA/Seattle". 你可以调用 Match(Uri, Uri) 来测试给定的候选 URI 是否与给定的 URI 模板匹配。Given a candidate URI, you can test whether it matches a given URI template by calling Match(Uri, Uri). 也可以调用 UriTemplateUri 来使用 BindByName(Uri, NameValueCollection) 实例根据一组变量值创建 BindByPosition(Uri, String[])You can also use UriTemplate instances to create a Uri from a set of variable values by calling BindByName(Uri, NameValueCollection) or BindByPosition(Uri, String[]).

构造函数

UriTemplate(String) UriTemplate(String) UriTemplate(String) UriTemplate(String)

使用指定的模板字符串初始化 UriTemplate 类的新实例。Initializes a new instance of the UriTemplate class with the specified template string.

UriTemplate(String, Boolean) UriTemplate(String, Boolean) UriTemplate(String, Boolean) UriTemplate(String, Boolean)

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

UriTemplate(String, Boolean, IDictionary<String,String>) UriTemplate(String, Boolean, IDictionary<String,String>) UriTemplate(String, Boolean, IDictionary<String,String>) UriTemplate(String, Boolean, IDictionary<String,String>)

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

UriTemplate(String, IDictionary<String,String>) UriTemplate(String, IDictionary<String,String>) UriTemplate(String, IDictionary<String,String>) UriTemplate(String, IDictionary<String,String>)

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

属性

Defaults Defaults Defaults Defaults

获取名称/值对的集合,其中元素为所有默认参数值。Gets a collection of name/value pairs for any default parameter values.

IgnoreTrailingSlash IgnoreTrailingSlash IgnoreTrailingSlash IgnoreTrailingSlash

指定在匹配候选 URI 时是否应忽略模板中的尾部正斜杠“/”。Specifies whether trailing slashes "/" in the template should be ignored when matching candidate URIs.

PathSegmentVariableNames PathSegmentVariableNames PathSegmentVariableNames PathSegmentVariableNames

获取模板的路径段中所使用的变量名的集合。Gets a collection of variable names used within path segments in the template.

QueryValueVariableNames QueryValueVariableNames QueryValueVariableNames QueryValueVariableNames

获取模板的查询字符串中所使用的变量名的集合。Gets a collection of variable names used within the query string in the template.

方法

BindByName(Uri, IDictionary<String,String>) BindByName(Uri, IDictionary<String,String>) BindByName(Uri, IDictionary<String,String>) BindByName(Uri, IDictionary<String,String>)

利用模板和参数集合创建一个新的 URI。Creates a new URI from the template and the collection of parameters.

BindByName(Uri, IDictionary<String,String>, Boolean) BindByName(Uri, IDictionary<String,String>, Boolean) BindByName(Uri, IDictionary<String,String>, Boolean) BindByName(Uri, IDictionary<String,String>, Boolean)

利用模板和参数集合创建一个新的 URI。Creates a new URI from the template and the collection of parameters.

BindByName(Uri, NameValueCollection) BindByName(Uri, NameValueCollection) BindByName(Uri, NameValueCollection) BindByName(Uri, NameValueCollection)

利用模板和参数集合创建一个新的 URI。Creates a new URI from the template and the collection of parameters.

BindByName(Uri, NameValueCollection, Boolean) BindByName(Uri, NameValueCollection, Boolean) BindByName(Uri, NameValueCollection, Boolean) BindByName(Uri, NameValueCollection, Boolean)

利用模板和参数集合创建一个新的 URI。Creates a new URI from the template and the collection of parameters.

BindByPosition(Uri, String[]) BindByPosition(Uri, String[]) BindByPosition(Uri, String[]) BindByPosition(Uri, String[])

利用模板和参数值数组创建一个新的 URI。Creates a new URI from the template and an array of parameter values.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

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

(Inherited from Object)
GetType() GetType() GetType() GetType()

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

(Inherited from Object)
IsEquivalentTo(UriTemplate) IsEquivalentTo(UriTemplate) IsEquivalentTo(UriTemplate) IsEquivalentTo(UriTemplate)

指示 UriTemplate 与其他模板的结构是否等效。Indicates whether a UriTemplate is structurally equivalent to another.

Match(Uri, Uri) Match(Uri, Uri) Match(Uri, Uri) Match(Uri, Uri)

尝试将 UriUriTemplate 匹配。Attempts to match a Uri to a UriTemplate.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

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

(Inherited from Object)
ToString() ToString() ToString() ToString()

返回 UriTemplate 实例的字符串表示形式。Returns a string representation of the UriTemplate instance.

适用于