Wednesday, October 15, 2014

System.Uri (.NET Framework 4.5) – A Visual Guide

Since I’ve repeated enough times the somewhat cumbersome exercise of inferring what the various properties of the .NET Framework (as of version 4.5) System.Uri class return for a “typical” HTTP/HTTPS URL from the official documentation – and generally resorted to manual testing to be 100% certain of the behavior – here’s a visual guide.

Given a System.Uri instance constructed as follows (C#):

System.Uri uri = new System.Uri("https://www.example.com:8080/dir1/dir2/page.aspx?param1=val1&param2=val2#anchor");

Here’s what the various relevant properties of that Uri instance return:

Property String value
AbsoluteUri
https://www.example.com:8080/dir1/dir2/page.aspx?param1=val1&param2=val2#anchor
OriginalString
https://www.example.com:8080/dir1/dir2/page.aspx?param1=val1&param2=val2#anchor
Scheme
https
Host
        www.example.com
Authority
        www.example.com:8080
Port
                        8080
AbsolutePath
                            /dir1/dir2/page.aspx                            
PathAndQuery
                            /dir1/dir2/page.aspx?param1=val1&param2=val2
Query
                                                ?param1=val1&param2=val2
Fragment
                                                                        #anchor

The Segments property returns the following String array:

String[] {
    "/",
    "dir1/",
    "dir2/",
    "page.aspx"
}

Hopefully this will save a little time for all of you as well as for me!

No comments:

Post a Comment

Non-spammers: Thanks for visiting! Please go ahead and leave a comment; I read them all!

Attention SPAMMERS: I review all comments before they get posted, and I REPORT 100% of spam comments to Google as spam! Why not avoid getting your account banned as quickly -- and save us both a little time -- by skipping this comment form and moving on to the next one on your list? Thanks, and I hope you have a great day!