C# System.Uri Class Examples

If you’ve needed to parse or construct a URI in C#, you’ve likely done it with the System.Uri class.

I put this script together to quickly show me the output of each of the System.Uri class methods. I used one of two test Uri’s. The first test Uri is a link to a web page, complete with every part of a Uri. The second is a link to an FTP server.

Snippet


using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
public static void Main()
{
/*

NOTE ABOUT ABSOLUTE PATHS:

Check Uri.IsAbsoluteUri
This property is true if the string or Uri instance that was passed into the constructor can be parsed as an absolute Uri instance, which contains a scheme, an authority, and a path. Otherwise, the Uri instance is treated as relative and might omit the scheme or other URI components.

*/

//string url = "https://some.cooldomain.com:3000/showcase/products/search?number=21&q=c%23+async+await&form=EDGEAR&qs=LS&cvid=4a865fdbf3ec4465a084c001c46a284f&cc=US&setlang=en-US&PC=DCTS#Section2";
string url = "https://some.cooldomain.com:3000/showcase/products/search.aspx?number=21&q=c%23+async+await&form=EDGEAR&qs=LS&cvid=4a865fdbf3ec4465a084c001c46a284f&cc=US&setlang=en-US&PC=DCTS#Section2";

//string url = "ftp://ftp2.census.gov/geo/tiger";

Uri u = new Uri(url);
Console.WriteLine("Testing the String.Uri class, using the following URL:");
Console.WriteLine(url);
Console.WriteLine(Environment.NewLine);
Console.WriteLine("//Get the absolute path of the Uri.  The path is part after the domain and before the querystring.  The absolute path starts at the web root.  NOTE: This is only valid for absolute URI's.  An exception is thrown if this is a realtive uri.");
Console.WriteLine("pathescaped is u.AbsolutePath = "+ u.AbsolutePath);
Console.WriteLine(Environment.NewLine);
Console.WriteLine("//Get the absolute URI. NOTE: This is only valid for absolute URI's.  An exception is thrown if this is a realtive uri.");
Console.WriteLine("uriescaped is  u.AbsoluteUri  = "+ u.AbsoluteUri);
Console.WriteLine(Environment.NewLine);
Console.WriteLine("//Get the Domain Name System (DNS) host name or IP address and the port number for a server. NOTE: This is only valid for absolute URI's.  An exception is thrown if this is a realtive uri.");
Console.WriteLine("hostplusport is  u.Authority  = "+ u.Authority);
Console.WriteLine(Environment.NewLine);
Console.WriteLine("//Get an unescaped host name that is safe to use for DNS resolution. NOTE: This is only valid for absolute URI's.  An exception is thrown if this is a realtive uri.");
Console.WriteLine("dnssafehost is  u.DnsSafeHost  = "+ u.DnsSafeHost);
Console.WriteLine(Environment.NewLine);
Console.WriteLine("//Get the host component of this instance. NOTE: This is only valid for absolute URI's.  An exception is thrown if this is a realtive uri.");
Console.WriteLine("host is  u.Host  = "+ u.Host);
Console.WriteLine(Environment.NewLine);
Console.WriteLine("//Get whether the Uri instance is absolute.");
Console.WriteLine("isabsoluteuri is  u.IsAbsoluteUri  = "+ u.IsAbsoluteUri);
Console.WriteLine(Environment.NewLine);
Console.WriteLine("//Get whether the port value of the URI is the default for this scheme.");
Console.WriteLine("isdefaultport is  u.IsDefaultPort = "+ u.IsDefaultPort);
Console.WriteLine(Environment.NewLine);
Console.WriteLine("//Get a value indicating whether the specified Uri is a file URI.");
Console.WriteLine("isfile is  u.IsFile  = "+ u.IsFile);
Console.WriteLine(Environment.NewLine);
Console.WriteLine("//Get whether the specified Uri references the local host.");
Console.WriteLine("islocal is  u.IsLoopback  = "+ u.IsLoopback);
Console.WriteLine(Environment.NewLine);
Console.WriteLine("//Get whether the specified Uri is a universal naming convention (UNC) path.");
Console.WriteLine("isunc is  u.IsUnc  = "+ u.IsUnc);
Console.WriteLine(Environment.NewLine);
Console.WriteLine("//Get a local operating-system representation of a file name. NOTE: Only valid for AbsoluteURI's.  An exception is thrown for relative Uri's.");
Console.WriteLine("pathunescaped is  u.LocalPath  = "+ u.LocalPath);
Console.WriteLine(Environment.NewLine);
Console.WriteLine("//Get the original URI string that was passed to the Uri constructor.   NOTE: Only valid for AbsoluteURI's.  An exception is thrown for relative Uri's.");
Console.WriteLine("originalstring is  u.OriginalString  = "+ u.OriginalString);
Console.WriteLine(Environment.NewLine);
Console.WriteLine("//Get the AbsolutePath and Query properties separated by a question mark (?).   NOTE: Only valid for AbsoluteURI's.  An exception is thrown for relative Uri's.");
Console.WriteLine("pathplusqueryescaped is  u.PathAndQuery  = "+ u.PathAndQuery);
Console.WriteLine(Environment.NewLine);
Console.WriteLine("//Get the port number of this URI.  NOTE: Only valid for AbsoluteURI's.  An exception is thrown for relative Uri's.");
Console.WriteLine("port is  u.Port  = "+ u.Port);
Console.WriteLine(Environment.NewLine);
Console.WriteLine("//Get any query information included in the specified URI.  NOTE: Only valid for AbsoluteURI's.  An exception is thrown for relative Uri's.");
Console.WriteLine("queryescaped is  u.Query  = "+ u.Query);
Console.WriteLine(Environment.NewLine);
Console.WriteLine("//Get the scheme name for this URI.  NOTE: Only valid for AbsoluteURI's.  An exception is thrown for relative Uri's.");
Console.WriteLine("protocol is  u.Scheme  = "+ u.Scheme);
Console.WriteLine("//Get the Fragment portion of this URI. NOTE: Only valid for AbsoluteURI's.  An exception is thrown for relative Uri's.");
Console.WriteLine("anchor is  u.Fragment  = "+ u.Fragment);
Console.WriteLine(Environment.NewLine);
Console.WriteLine("//Get an array containing the path segments that make up the specified URI.  NOTE: Only valid for AbsoluteURI's.  An exception is thrown for relative Uri's.");
Console.WriteLine("segments is  u.Segments = "+ u.Segments);
for(int i = 0; i <  u.Segments.Length; i++)
{
Console.WriteLine( u.Segments[i]);
}

Console.WriteLine(Environment.NewLine);
Console.WriteLine("//Indicates that the URI string was completely escaped before the Uri instance was created.");
Console.WriteLine("userescaped is  u.UserEscaped  = "+ u.UserEscaped);
Console.WriteLine(Environment.NewLine);
Console.WriteLine("//Get the user name, password, or other user-specific information associated with the specified URI.");
Console.WriteLine("//NOTE: while this: http://username:password@example.com/ is technically possible, it is no longer supported by IE or Chrome and so probably others. ");
Console.WriteLine("//NOTE: Only valid for AbsoluteURI's.  An exception is thrown for relative Uri's.");"
Console.WriteLine("userinfo is  u.UserInfo  = "+ u.UserInfo);
}
}

Output


Testing the String.Uri class, using the following URL:
https://some.cooldomain.com:3000/showcase/products/search.aspx?number=21&q=c%23+async+await&form=EDGEAR&qs=LS&cvid=4a865fdbf3ec4465a084c001c46a284f&cc=US&setlang=en-US&PC=DCTS#Section2

//Get the absolute path of the Uri.  The path is part after the domain and before the querystring.  The absolute path starts at the web root.
pathescaped is u.AbsolutePath = /showcase/products/search.aspx

//Get the absolute URI.
uriescaped is  u.AbsoluteUri  = https://some.cooldomain.com:3000/showcase/products/search.aspx?number=21&q=c%23+async+await&form=EDGEAR&qs=LS&cvid=4a865fdbf3ec4465a084c001c46a284f&cc=US&setlang=en-US&PC=DCTS#Section2

//Get the Domain Name System (DNS) host name or IP address and the port number for a server.
hostplusport is  u.Authority  = some.cooldomain.com:3000

//Get an unescaped host name that is safe to use for DNS resolution.
dnssafehost is  u.DnsSafeHost  = some.cooldomain.com

//Get the host component of this instance.
host is  u.Host  = some.cooldomain.com

//Get whether the Uri instance is absolute.
isabsoluteuri is  u.IsAbsoluteUri  = True

//Get whether the port value of the URI is the default for this scheme.
isdefaultport is  u.IsDefaultPort = False

//Get a value indicating whether the specified Uri is a file URI.
isfile is  u.IsFile  = False

//Get whether the specified Uri references the local host.
islocal is  u.IsLoopback  = False

//Get whether the specified Uri is a universal naming convention (UNC) path.
isunc is  u.IsUnc  = False

//Get a local operating-system representation of a file name.
pathunescaped is  u.LocalPath  = /showcase/products/search.aspx

//Get the original URI string that was passed to the Uri constructor.
originalstring is  u.OriginalString  = https://some.cooldomain.com:3000/showcase/products/search.aspx?number=21&q=c%23+async+await&form=EDGEAR&qs=LS&cvid=4a865fdbf3ec4465a084c001c46a284f&cc=US&setlang=en-US&PC=DCTS#Section2

//Get the AbsolutePath and Query properties separated by a question mark (?).
pathplusqueryescaped is  u.PathAndQuery  = /showcase/products/search.aspx?number=21&q=c%23+async+await&form=EDGEAR&qs=LS&cvid=4a865fdbf3ec4465a084c001c46a284f&cc=US&setlang=en-US&PC=DCTS

//Get the port number of this URI.
port is  u.Port  = 3000

//Get any query information included in the specified URI.
queryescaped is  u.Query  = ?number=21&q=c%23+async+await&form=EDGEAR&qs=LS&cvid=4a865fdbf3ec4465a084c001c46a284f&cc=US&setlang=en-US&PC=DCTS

//Get the scheme name for this URI.
protocol is  u.Scheme  = https

//Get an array containing the path segments that make up the specified URI.
segments is  u.Segments = System.String[]
/
showcase/
products/
search.aspx

//Indicates that the URI string was completely escaped before the Uri instance was created.
userescaped is  u.UserEscaped  = False

//Get the user name, password, or other user-specific information associated with the specified URI.
//NOTE: while this: http://username:password@example.com/ is technically possible, it is no longer supported by IE or Chrome and so probably others.
userinfo is  u.UserInfo  =

**************** FTP EXAMPLE ************

Testing the String.Uri class, using the following URL:
ftp://ftp2.census.gov/geo/tiger

//Get the absolute path of the URI.
pathescaped is u.AbsolutePath = /geo/tiger

//Get the absolute URI.
uriescaped is  u.AbsoluteUri  = ftp://ftp2.census.gov/geo/tiger

//Get the Domain Name System (DNS) host name or IP address and the port number for a server.
hostplusport is  u.Authority  = ftp2.census.gov

//Get an unescaped host name that is safe to use for DNS resolution.
dnssafehost is  u.DnsSafeHost  = ftp2.census.gov

//Get the host component of this instance.
host is  u.Host  = ftp2.census.gov

//Get whether the Uri instance is absolute.
isabsoluteuri is  u.IsAbsoluteUri  = True

//Get whether the port value of the URI is the default for this scheme.
isdefaultport is  u.IsDefaultPort = True

//Get a value indicating whether the specified Uri is a file URI.
isfile is  u.IsFile  = False

//Get whether the specified Uri references the local host.
islocal is  u.IsLoopback  = False

//Get whether the specified Uri is a universal naming convention (UNC) path.
isunc is  u.IsUnc  = False

//Get a local operating-system representation of a file name.
pathunescaped is  u.LocalPath  = /geo/tiger

//Get the original URI string that was passed to the Uri constructor.
originalstring is  u.OriginalString  = ftp://ftp2.census.gov/geo/tiger

//Get the AbsolutePath and Query properties separated by a question mark (?).
pathplusqueryescaped is  u.PathAndQuery  = /geo/tiger

//Get the port number of this URI.
port is  u.Port  = 21

//Get any query information included in the specified URI.
queryescaped is  u.Query  =

//Get the scheme name for this URI.
protocol is  u.Scheme  = ftp

//Get an array containing the path segments that make up the specified URI.
segments is  u.Segments = System.String[]
/
geo/
tiger

//Indicates that the URI string was completely escaped before the Uri instance was created.
userescaped is  u.UserEscaped  = False

//Get the user name, password, or other user-specific information associated with the specified URI.
//NOTE: while this: http://username:password@example.com/ is technically possible, it is no longer supported by IE or Chrome and so probably others.
userinfo is  u.UserInfo  =

Posted in C#

Question or comment?