BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Network and Internet
.NET 1.1+

The System.Uri Class

Software that uses networked resources, web sites or remote files often makes use of Uniform Resource Identifiers to locate those resources. Although these can be held in simple strings, using the System.Uri class provides many additional benefits.

URI Flags

The Uri class defines several useful Boolean properties that allow you to quickly categorise a URI. They are:

  • IsAbsoluteUri. This property is true if the URI represented by the object is absolute and false if it is relative. The property was introduced in version 2.0 of the .NET framework.
  • IsDefaultPort. This property is true if the port number is not present, or if it is set to the default port number expected by the URI scheme. When an alternative port number has been specified, the return value is false.
  • IsFile. This property is set to true for file paths and false for all other URI schemes.
  • IsLoopback. This property returns true for URIs that are local. This includes URIs such as "http://localhost" and "127.0.0.1".
  • IsUnc. This property returns true when the Uri holds a UNC path.
Uri uri = new Uri(
    "http://bob:password@authority:5555/path/page.aspx?value1=10&value2=20#fragment");
Console.WriteLine(uri.IsAbsoluteUri);
Console.WriteLine(uri.IsDefaultPort);
Console.WriteLine(uri.IsFile);
Console.WriteLine(uri.IsLoopback);
Console.WriteLine(uri.IsUnc);

/* OUTPUT

True
False
False
False
False

*/

URI Segments

We've seen how the path part of a URI can be obtained using the Path property. When you wish to process a path from an absolute URI, perhaps performing some URL rewriting on a server, you need a more fine-grained way of reading the path. The Segments property can help. This property returns the path as an array of strings. Each string holds one segment of the path, where the segments are separated by forward slashes.

The code below shows the Segments property in action. Here the path has two parts but generates three segments. The first segment is a simple forward slash.

Uri uri = new Uri(
    "http://bob:password@authority:5555/path/page.aspx?value1=10&value2=20#fragment");
foreach (string segment in uri.Segments)
{
    Console.WriteLine(segment);
}

/* SEGMENTS

/
path/
page.aspx

*/

Uri Methods

The constructors and properties of the Uri class make working with URIs much simpler than if you tried to achieve the same results with strings alone. For yet more flexibility, the type includes a number of instance and static methods. These will be the focus of the remaining sections of this article.

Comparing URIs to Obtain a Relative Path

If you want a resource at one URI to refer to another URI using the relative syntax, you may need to convert an absolute URI to a relative one. This is the purpose of the MakeRelativeUri method. This is called on one absolute URI, passing another as an argument. The method works out the path that must be followed from the first URI to reach the second and returns this as a new, relative Uri object. If the path between the two addresses requires moving "upwards" through the path hierarchy, the upwards steps will be included as ".." segments in the result.

You can see this in the sample code below:

Uri uri1 = new Uri("http://www.blackwasp.co.uk/Uri.aspx");
Uri uri2 = new Uri("http://www.blackwasp.co.uk/images/RacingCar.gif");

Console.WriteLine(uri1.MakeRelative(uri2));
Console.WriteLine(uri2.MakeRelative(uri1));

/* OUTPUT

images/RacingCar.gif
../Uri.aspx

*/

NB: The MakeRelative method was deprecated in .NET version 2.0. You can achieve similar results using a new method, "MakeRelativeUri".

Extracting the Left Side of the URI

The Uri properties allow you to extract parts of a URI in many ways. Yet another way to get this information is with the GetLeftPart method. This retrieves one or more parts from a URI, starting with the scheme and working from left to right. You determine which is the last part to return by passing a value from the UriPartial enumeration. You can stop with the scheme, authority, path or query. All four options are shown in the next sample.

Uri uri = new Uri(
    "http://bob:password@authority:5555/path/page.aspx?value1=10&value2=20#fragment");
Console.WriteLine(uri.GetLeftPart(UriPartial.Scheme));
Console.WriteLine(uri.GetLeftPart(UriPartial.Authority));
Console.WriteLine(uri.GetLeftPart(UriPartial.Path));
Console.WriteLine(uri.GetLeftPart(UriPartial.Query));

/* OUPUT

http://
http://bob:password@authority:5555
http://bob:password@authority:5555/path/page.aspx
http://bob:password@authority:5555/path/page.aspx?value1=10&value2=20

*/
20 June 2012