UriTemplate.Match(Uri, Uri) 方法
定义
尝试将 Uri 与 UriTemplate 匹配。Attempts to match a Uri to a UriTemplate.
public:
UriTemplateMatch ^ Match(Uri ^ baseAddress, Uri ^ candidate);
public UriTemplateMatch Match (Uri baseAddress, Uri candidate);
member this.Match : Uri * Uri -> UriTemplateMatch
Public Function Match (baseAddress As Uri, candidate As Uri) As UriTemplateMatch
参数
- baseAddress
- Uri
基址。The base address.
返回
一个实例。An instance.
示例
下面的示例演示如何调用 Match(Uri, Uri) 方法。The following example shows how to call the Match(Uri, Uri) method.
UriTemplate template = new UriTemplate("weather/{state}/{city}?forecast={day}");
Uri prefix = new Uri("http://localhost");
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")
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 的通配符段来填充 UriTemplateMatch。If the match is successful the UriTemplateMatch is populated with the URI segments, variable values, query string values, and wildcard segments from the candidate URI. 如果匹配失败,则返回 null。If the match is unsuccessful, null is returned.