UriTemplate Class


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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

Namespace:   System
Assembly:  System.ServiceModel (in System.ServiceModel.dll)


public class UriTemplate


Initializes a new instance of the UriTemplate class with the specified template string.

System_CAPS_pubmethodUriTemplate(String, Boolean)

Initializes a new instance of the UriTemplate class.

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

Initializes a new instance of the UriTemplate class.

System_CAPS_pubmethodUriTemplate(String, IDictionary<String, String>)

Initializes a new instance of the UriTemplate class.


Gets a collection of name/value pairs for any default parameter values.


Specifies whether trailing slashes “/” in the template should be ignored when matching candidate URIs.


Gets a collection of variable names used within path segments in the template.


Gets a collection of variable names used within the query string in the template.

System_CAPS_pubmethodBindByName(Uri, IDictionary<String, String>)

Creates a new URI from the template and the collection of parameters.

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

Creates a new URI from the template and the collection of parameters.

System_CAPS_pubmethodBindByName(Uri, NameValueCollection)

Creates a new URI from the template and the collection of parameters.

System_CAPS_pubmethodBindByName(Uri, NameValueCollection, Boolean)

Creates a new URI from the template and the collection of parameters.

System_CAPS_pubmethodBindByPosition(Uri, String[])

Creates a new URI from the template and an array of parameter values.


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)


Serves as the default hash function. (Inherited from Object.)


Gets the Type of the current instance.(Inherited from Object.)


Indicates whether a UriTemplate is structurally equivalent to another.

System_CAPS_pubmethodMatch(Uri, Uri)

Attempts to match a T:System.URI to a UriTemplate.


Creates a shallow copy of the current Object.(Inherited from Object.)


Returns a string representation of the UriTemplate instance.(Overrides Object.ToString().)

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");

foreach (string name in template.PathSegmentVariableNames)
    Console.WriteLine("     {0}", name);

foreach (string name in template.QueryValueVariableNames)
    Console.WriteLine("     {0}", name);

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
Available since 3.5
Available since 4.0

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top