Three way to form URI for REST Services

Objective

In this article, I will show how we could construct URI in three ways.

Method # 1: Creating URI from string

I am passing string in constructor of URI class, to create new URI.

clip_image002

Output

clip_image004

Method #2 New URI from Component Instance

I am here taking host and path individually and then combining them in constructor of Uri class.

clip_image006

Output

clip_image008

Method #3 Creating URI using Try Pattern validation

In this , I am showing you how could we use Try Pattern method of URI class to construct an URI.

clip_image010

Output

clip_image012

The full source code for your reference is given below

Program.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Net;

namespace ConsoleApplication5

{

class Program

{

static void Main(string[] args)

{

#region Creating URI from string

string url = http://dhananjaykumar.net/feed/”;

Uri uri = new Uri(url);

Console.WriteLine(uri.AbsoluteUri);

Console.Read();

#endregion

#region Creating URI from Component Instance

string host = http://dhananjaykumar.net”;

string path = “/feed”;

Uri uri1 = new Uri(host);

uri1 = new Uri(uri1, path);

Console.WriteLine(uri1.AbsoluteUri);

Console.Read();

#endregion

#region Create New URI Try Pattern Validation

string url = http://dhananjaykumar.net/feed/”;

Uri uri2;

if(Uri.TryCreate(url,UriKind.Absolute , out uri2))

{

Console.WriteLine(uri2);

}

Console.Read();

#endregion

Console.Read();

}

}

}


Dhananjay Kumar is Developer, Blogger , Speaker, Learner , Mindcracker & Microsoft MVP.

Posted in REST Services
One comment on “Three way to form URI for REST Services
  1. Vishal says:

    I see you blog a lot about WCF Data Services and RESTful services.
    Check out this new product my company has introduced. Its called SO-Aware.

    http://www.tellagostudios.com/products/so-aware%E2%84%A2

    The express edition is free.. Try it. We would appreciate any valuable feedback. Also please pass the word to WCF folks you know in India.

    Thanks,

    Vishal

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 )

Twitter picture

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

Facebook photo

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

Connecting to %s

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Categories
Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my current or previous employer's view in anyway. © Copyright 2013
Follow

Get every new post delivered to your Inbox.

Join 2,134 other followers

%d bloggers like this: