1 out of 1 rated this helpful - Rate this topic

UriTemplate Class

A class that represents a Uniform Resource Identifier (URI) template.

System.Object
  System.UriTemplate

Namespace:  System
Assembly:  System.ServiceModel (in System.ServiceModel.dll)
public class UriTemplate

The UriTemplate type exposes the following members.

  Name Description
Public method UriTemplate(String) Initializes a new instance of the UriTemplate class with the specified template string.
Public method UriTemplate(String, Boolean) Initializes a new instance of the UriTemplate class.
Public method UriTemplate(String, IDictionary<String, String>) Initializes a new instance of the UriTemplate class.
Public method UriTemplate(String, Boolean, IDictionary<String, String>) Initializes a new instance of the UriTemplate class.
Top
  Name Description
Public property Defaults Gets a collection of name/value pairs for any default parameter values.
Public property IgnoreTrailingSlash Specifies whether trailing slashes “/” in the template should be ignored when matching candidate URIs.
Public property PathSegmentVariableNames Gets a collection of variable names used within path segments in the template.
Public property QueryValueVariableNames Gets a collection of variable names used within the query string in the template.
Top
  Name Description
Public method BindByName(Uri, IDictionary<String, String>) Creates a new URI from the template and the collection of parameters.
Public method BindByName(Uri, NameValueCollection) Creates a new URI from the template and the collection of parameters.
Public method BindByName(Uri, IDictionary<String, String>, Boolean) Creates a new URI from the template and the collection of parameters.
Public method BindByName(Uri, NameValueCollection, Boolean) Creates a new URI from the template and the collection of parameters.
Public method BindByPosition Creates a new URI from the template and an array of parameter values.
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method IsEquivalentTo Indicates whether a UriTemplate is structurally equivalent to another.
Public method Match Attempts to match a URI to a UriTemplate.
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns a string representation of the UriTemplate instance. (Overrides Object.ToString().)
Top

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[]).

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]);
    }
}


.NET Framework

Supported in: 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Check your versions if you can't see this in System.ServiceModel.dll
The UriTemplate class is implemented in the System.ServiceModel.Web.dll assembly in .NET 3.5.