Uri Template in WCF REST Service:Part #1

No.of Views1173
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  14 Jun 2010 10:06:51
Tag : WCF , REST Services
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.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Objective

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.

UriTemplate class stucture

Image Loading

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

 

Image Loading

Let us say, URI is
http://localhost:8000/MyService/{A}/{B}/{C}/{D}
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.

 

Image Loading

Sample #1

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

 

Image Loading

Explanation


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.

Output  

Image Loading

Sample #2

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

Image Loading

Explanation

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.

Output
 

Image Loading

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. 

Image Loading

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,

Program.CS

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)
            {
                Console.WriteLine(r);
            }
            Console.Read();#endregion#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]);
                }
            }else{
                Console.WriteLine("BASE URI is not same ");
            }

            Console.Read();#endregion 


        }
    }
}

 

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top