UriTemplate Class
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
A class that represents a Uniform Resource Identifier (URI) template.
public ref class UriTemplate
public class UriTemplate
type UriTemplate = class
Public Class UriTemplate
- Inheritance
-
UriTemplate
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
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. 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/{state}/{city}"
"weather/*"
"weather/{state}/{city}?forecast=today
"weather/{state}/{city}?forecast={day}
The preceding URI templates might be used for organizing weather reports. Segments enclosed in curly braces are variables, everything else is a literal. You can convert a UriTemplate instance into a Uri by replacing variables with actual values. For example, taking the template "weather/{state}/{city}" and putting in values for the variables "{state}" and "{city}" gives you "weather/WA/Seattle". Given a candidate URI, you can test whether it matches a given URI template by calling Match(Uri, Uri). 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[]).
Uri |
Initializes a new instance of the UriTemplate class. |
Uri |
Initializes a new instance of the UriTemplate class. |
Uri |
Initializes a new instance of the UriTemplate class. |
Uri |
Initializes a new instance of the UriTemplate class with the specified template string. |
Defaults |
Gets a collection of name/value pairs for any default parameter values. |
Ignore |
Specifies whether trailing slashes "/" in the template should be ignored when matching candidate URIs. |
Path |
Gets a collection of variable names used within path segments in the template. |
Query |
Gets a collection of variable names used within the query string in the template. |
Bind |
Creates a new URI from the template and the collection of parameters. |
Bind |
Creates a new URI from the template and the collection of parameters. |
Bind |
Creates a new URI from the template and the collection of parameters. |
Bind |
Creates a new URI from the template and the collection of parameters. |
Bind |
Creates a new URI from the template and an array of parameter values. |
Equals(Object) |
Determines whether the specified object is equal to the current object. (Inherited from Object) |
Get |
Serves as the default hash function. (Inherited from Object) |
Get |
Gets the Type of the current instance. (Inherited from Object) |
Is |
Indicates whether a UriTemplate is structurally equivalent to another. |
Match(Uri, Uri) |
Attempts to match a Uri to a UriTemplate. |
Memberwise |
Creates a shallow copy of the current Object. (Inherited from Object) |
To |
Returns a string representation of the UriTemplate instance. |
Produkt | Versionen |
---|---|
.NET Framework | 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
Feedback zu .NET
.NET ist ein Open Source-Projekt. Wählen Sie einen Link aus, um Feedback zu geben: