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
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
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.
Sample #1
In this sample, I will display the entire Path Segment key from the URI.
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
Sample #2
In this sample, we will see how we can access and print all the Path Segment
keys and values from an UriTemplate.
Explanation
- Using Match method of UriTemplateclass, we are matching both base Uri and Uri
entered by the user.
- If both Uri does not match then Match method will return null.
- Else, we are saving the return value in reference of UriTemplateMatch class.
- Calling the BoundVariablesproperties and saving in a variable.
- Iterating through all the keys and displaying the keys and values.
Output
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,
Program.CS
using
System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.ServiceModel;
usingSystem.ServiceModel.Description;
usingSystem.ServiceModel.Web;
using
Contracts;
namespaceSelfHostedRESTService
{
classProgram
{
staticvoid
Main(string[] args)
{
#region
Checking Path Segment
UribaseUri
= newUri("http://Localhost:8000");
UriTemplate
template = newUriTemplate("/{A}/{B}/{C}/{D}");
Console.WriteLine("URI
Path segments are ");
foreach
(var r intemplate.PathSegmentVariableNames)
{
Console.WriteLine(r);
}
Console.Read();
#endregion
#regionChecking
the values
Console.WriteLine("Type
here a URI to Find their value");
stringuserUri
= Console.ReadLine();
UritestUri
= newUri(userUri);
UriTemplateMatch
match = template.Match(baseUri, testUri);
if
(match != null)
{
var
bound = match.BoundVariables;
stringkeyValues;
foreach
(var r inbound.Keys)
{
keyValues = r.ToString();
Console.WriteLine("{0}={1}",
keyValues, bound[keyValues]);
}
}
else
{
Console.WriteLine("BASE
URI is not same ");
}
Console.Read();
#endregion
}
}
}