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


Top 5 Contributors of the Month
david stephan

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

 Subscribe to Articles

Three way to form URI for REST Services

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

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

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. 


1.gif

Codeview of the above image

#region Creating URI from string 
string url = "http://dhananjaykumar.net/feed/";
Uri uri = new Uri(url);
Console.WriteLine(uri.AbsoluteUri);      
Console.Read();
#endregion 


Output


2.gif 


Method #2: New URI from Component Instance 


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


3.gif

Codeview of the above image
#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 


Output


4.gif 


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. 


5.gif

Codeview of the above image

#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 

Output

6.gif

You can also download the code from below link

 Subscribe to Articles

     

Further Readings:

Responses

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