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.

Combining Absolute and Relative URIs

As described previously, relative URIs are commonly encountered but must be combined with an absolute URI in order to be useful. We can perform such combinations with another pair of constructors. When instantiating a Uri object we can provide an absolute Uri as the first argument and a relative URI as the second, using either a second Uri or a string. The resultant Uri will hold the relative URI's path with the scheme and authority from the absolute one.

Uri baseUri = new Uri("http://www.blackwasp.co.uk");
Uri relativeUri = new Uri("Uri.aspx", UriKind.Relative);
Uri combined = new Uri(baseUri, relativeUri);
Console.WriteLine(combined);

/* OUTPUT

http://www.blackwasp.co.uk/Uri.aspx

*/

A nice feature of this constructor is that the forward slash between the authority and path is correctly added. In the above code no slash was present in either URI but the slash was added. In the sample below, a trailing slash exists in the absolute URI and a leading slash prefixes the relative URI. The combined result has a single slash, a desirable result.

Uri baseUri = new Uri("http://www.blackwasp.co.uk/");
Uri relativeUri = new Uri("/Uri.aspx", UriKind.Relative);
Uri combined = new Uri(baseUri, relativeUri);
Console.WriteLine(combined);

/* OUTPUT

http://www.blackwasp.co.uk/Uri.aspx

*/

Finally, consider the following sample. Here the absolute URI includes a path. This may be a source web page that contains a relative link. When combined, the path from the absolute URI is discarded.

Uri baseUri = new Uri("http://www.blackwasp.co.uk/Source.aspx");
Uri relativeUri = new Uri("Destination.aspx", UriKind.Relative);
Uri combined = new Uri(baseUri, relativeUri);
Console.WriteLine(combined);

/* OUTPUT

http://www.blackwasp.co.uk/Destination.aspx

*/

Extracting Uri Information

The Uri constructors are useful in themselves but much more functionality is available through the class' members. In this section we'll look at some of the properties that can be used to extract parts of a URI. To begin, let's create a Uri that contains all of the parts described at the beginning of this article. We'll then set about extracting those parts individually and in groups.

Uri uri = new Uri(
    "http://bob:password@authority.com:5555/path/page.aspx?value1=10&value2=20#fragment");

If you are going to act upon a resource by loading it using installed software, you may need to obtain the scheme in order to determine which software to use. You can get this as a string using the Scheme property:

Console.WriteLine(uri.Scheme);  // http

The authority is obtained using the Authority property. Again, this returns a string. Note that it does not include the port number or user information.

Console.WriteLine(uri.Authority);  // authority.com

The user details are found with the UserInfo property. There is no property to get the user name or password individually. However, they can easily be separated using standard string methods, such as Split.

Console.WriteLine(uri.UserInfo);  // bob:password

The port number is obtained using the Port property, which returns an integer.

Console.WriteLine(uri.Port);  // 5555

To obtain the path of a URI as a single string, read the AbsolutePath property. This returns everything from the opening forward slash of the path to the beginning of any query.

Console.WriteLine(uri.AbsolutePath);  // /path/page.aspx

The query is retrieved using the Query property. As you can see in the code below, the returned value includes the initial question mark.

Console.WriteLine(uri.Query);  // ?value1=10&value2=20

If you need to get the fragment, use the Fragment property. As with the query, the separator character, this time a hash, is included in the result.

Console.WriteLine(uri.Fragment);  // #fragment

Another useful property is Host. This returns the host part of the URI, which we haven't mentioned before. The host is usually the name of a server, a DNS name or an IP address. It is part of the authority but does not include the port number or user information.

Console.WriteLine(uri.Host);  // authority

When retrieving the host name it's often important to know what type of host it is. For example, you may wish to treat an IP address differently than a DNS name. Rather than parse the string and apply rules to determine the host type you can read the HostNameType property. This returns a value from the UriHostNameType enumeration. The possible values are:

  • Unknown. The host name type is not present in Uri.
  • Basic. The host name type is present but can't be determined
  • Dns. The host name is for a DNS host, such as "blackwasp.co.uk".
  • IPv4. The host is an IP address, such as "192.168.0.1".
  • IPv6. The host is an IP version 6 address, such as "2001:0DB8:AC10:FE01::".
Console.WriteLine(uri.HostNameType);  // Dns

Sometimes you will want to obtain both the path and the query parts of a URI. Although you could use the Path and Query properties, it can be better to use the PathAndQuery property, which returns the information as a string.

Console.WriteLine(uri.PathAndQuery);  // /path/page.aspx?value1=10&value2=20

The final property we'll use to get URI information is LocalPath. This property is useful when you have a URI that represents a file path. The URI provides a standard format for this that is independent of the operating system. LocalPath converts the URI to a path that could be used in the local file system.

Uri uri = new Uri("file://c:/windows/system/vga.drv");
Console.WriteLine(uri.LocalPath);

/* OUTPUT

c:\windows\system\vga.drv

*/
20 June 2012