This topic has not yet been rated - Rate this topic

UriTemplateTable Class

A class that represents an associative set of UriTemplate objects.

System.Object
  System.UriTemplateTable

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

The UriTemplateTable type exposes the following members.

  Name Description
Public method UriTemplateTable() Initializes a new instance of the UriTemplateTable class.
Public method UriTemplateTable(Uri) Initializes a new instance of the UriTemplateTable class with the specified base address.
Public method UriTemplateTable(IEnumerable<KeyValuePair<UriTemplate, Object>>) Initializes a new instance of the UriTemplateTable class with the specified collection of key/value pairs.
Public method UriTemplateTable(Uri, IEnumerable<KeyValuePair<UriTemplate, Object>>) Initializes a new instance of the UriTemplateTable class with the specified base address and collection of key/value pairs.
Top
  Name Description
Public property BaseAddress Gets and sets the base address for the UriTemplateTable instance.
Public property IsReadOnly Gets a value that specifies whether the UriTemplateTable is read only.
Public property KeyValuePairs Gets a collection of key/value pairs that consist of UriTemplate objects and their associated data.
Public property OriginalBaseAddress Gets the original base address.
Top
  Name Description
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 MakeReadOnly Makes the UriTemplateTable read only.
Public method Match Attempts to match a candidate Uri to the UriTemplateTable.
Public method MatchSingle Attempts to match a candidate Uri to the UriTemplateTable.
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Top

A UriTemplateTable is an associative set of UriTemplate objects bound to an object of the developer's choosing. It allows you to match candidate Uniform Resource Identifiers (URIs) against the templates in the set and retrieve the data associated with the matching templates. The contents of UriTemplateTable can be changed until the MakeReadOnly(Boolean) method is called, at which time one of following types of validation occurs:

  • When MakeReadOnly(Boolean) is called passing in false, the UriTemplateTable checks to make sure the table contains no multiple structurally-equivalent templates. If it finds such templates, it throws an exception. This type of validation is used in conjunction with MatchSingle(Uri) when you want to ensure only one template matches an incoming URI.

  • When MakeReadOnly(Boolean) is called passing in true, multiple structurally-equivalent templates can be contained within a UriTemplateTable. However, any query strings in the templates must not be ambiguous; identical query strings are allowed. For more information about ambiguous query strings, see UriTemplate and UriTemplateTable.

The following code shows how to create a UriTemplateTable, populate it, and use it to match against a candidate Uri.


Uri prefix = new Uri("http://localhost/");

//Create a series of templates
UriTemplate weatherByCity  = new UriTemplate("weather/ state}/ city}");
UriTemplate weatherByCountry = new UriTemplate("weather/ country}/ village}");       
UriTemplate weatherByState = new UriTemplate("weather/ state}");
UriTemplate traffic = new UriTemplate("traffic/*");
UriTemplate wildcard = new UriTemplate("*");

//Create a template table
UriTemplateTable table = new UriTemplateTable(prefix);
//Add each template to the table with some associated data
table.KeyValuePairs.Add(new KeyValuePair<UriTemplate, Object>(weatherByCity, "weatherByCity"));
table.KeyValuePairs.Add(new KeyValuePair<UriTemplate, Object>(weatherByCountry, "weatherByCountry"));
table.KeyValuePairs.Add(new KeyValuePair<UriTemplate, Object>(weatherByState, "weatherByState"));
table.KeyValuePairs.Add(new KeyValuePair<UriTemplate, Object>(traffic, "traffic"));

table.MakeReadOnly(true);
Console.WriteLine("KeyValuePairs:");
foreach (KeyValuePair<UriTemplate, Object> keyPair in table.KeyValuePairs)
{
    Console.WriteLine("     0},  1}", keyPair.Key, keyPair.Value);
}

Console.WriteLine();

//Call MatchSingle to retrieve some match results:
ICollection<UriTemplateMatch> results = null;
Uri weatherInSeattle = new Uri("http://localhost/weather/Washington/Seattle");

results = table.Match(weatherInSeattle);
if( results != null)
{
    Console.WriteLine("Matching templates:");
    foreach (UriTemplateMatch match in results)
    {
        Console.WriteLine("    0}", match.Template);
    }
}


.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
Missing open braces
I don't usually create these programmatically, but I believe an opening brace is missing (replaced with a space) for all of the UriTemplate creations at the top, in the C# code.