.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Articles >> WCF >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Uri Template in WCF REST Service Part 1

Posted By:Dhananjay Kumar       Posted Date: June 02, 2010    Points: 25    Category: WCF    URL: http://www.dotnetspark.com  

Uri Template in WCF REST Service. This article will give explanation about UriTemplate class in WCF REST Service. We will see how UriTemplate class helps us to construct the URI for the methods.

Uri Template in WCF REST Service Part#1


This article will give explanation about UriTemplate class in WCF REST Service.  We will see how UriTemplate class helps us to construct the URI for the methods.

UriTemplate class

This class is used to construct URI for the methods in WCF REST Service.  It is inside the namespace  System.ServiceModel.Web.

This article is also published in my Blog

UriTemplate class

How it works?

Step1  UriTemplate creates a template out of URI

Step2  It takes URI as input parameter

Step3   Parses the input URI

Step4  Check whether pattern matches URI

Step5  If Yes then saves the matched pattern into data structure indexed by either order or name

Let us say, URI is


In above URI, all the part in curly braces is known as PATH SEGEMENT of URI.  We can find Path Segment of URI using code from a URI. Part of the URI before path segment is called base address of the URI. UriTemplate matches the base address, if it matches then it save the path segment in data structure indexed by either name or order. We can fetch this data structure to get the path segment as key value pair.

Sample #1

In this sample, I will display the entire Path Segment key from the URI.


1.  Assigning the base address to baseUri variable.

2.  Using UriTemplate class, creating the UriTemplate for baseUri.

3.  Using PathSegmentVariableNames property accessing all the Path segment variables in for each statement and displaying that.


Sample #2

In this sample, we will see how we can access and print all the Path Segment keys and values from an UriTemplate.


1.  Using Match method of UriTemplate class, we are matching both base Uri and Uri entered by the user.

2.  If both Uri does not match then Match method will return null.

3.  Else, we are saving the return value in reference of UriTemplateMatch class.

4.  Calling the BoundVariables properties and saving in a variable.

5.  Iterating through all the keys and displaying the keys and  values.


If you see the above output we are getting key value pair from the Path segment.  Now try to give same URL but do not pass values for all four Path Segments.

We can see that even if base Uri is same we are getting the output URI is not same because we are not passing value for all the four Path segments. So to solve this problem we have something called UriTemplateTable.  We will see this in next article. I hope this post was useful. Thanks for reading. Happy Coding.
For your reference, source code is as below,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Web;
using Contracts; 

namespace SelfHostedRESTService
    class Program
        static void Main(string[] args)

            #region Checking Path Segment

            Uri baseUri = new Uri("http://Localhost:8000");
            UriTemplate template = new UriTemplate("/{A}/{B}/{C}/{D}");
            Console.WriteLine("URI Path segments are ");
            foreach (var r in template.PathSegmentVariableNames)


            #region Checking the values 

            Console.WriteLine("Type here a URI to Find their value");
            string userUri = Console.ReadLine();
            Uri testUri = new Uri(userUri);
            UriTemplateMatch match = template.Match(baseUri, testUri);
            if (match != null)
                var bound = match.BoundVariables;
                string keyValues;
                foreach (var r in bound.Keys)
                    keyValues = r.ToString();
                    Console.WriteLine("{0}={1}", keyValues, bound[keyValues]);
                Console.WriteLine("BASE URI is not same ");




 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend