Uri Template in WCF REST Service Part#1

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

clip_image002

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

clip_image004

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.

clip_image006

Sample #1

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

clip_image008

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

clip_image010

Sample #2

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

clip_image012

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

clip_image014

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.

clip_image016

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

}

}

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com