O'Reilly logo

C# in a Nutshell by Peter Drayton, Ted Neward, Ben Albahari

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

Uri

Synopsis

This class encapsulates a complete URI (Uniform Resource Identifier) and provides various parts of it through properties. For example, you can get the Scheme (e.g., http, https, mailto) and the Port number. For http, the default port is 80, if not specified in the Uri (ftp uses port 21, https uses 443, and mailto uses 25). You can also retrieve the query-string arguments — including the initial question mark — from the Query property, or the fragment portion — including the fragment marker (#) — from the Fragment property. Some boolean properties include IsLoopback, which indicates True if the Uri references the local host, and IsUnc, which indicates True if the Uri is a UNC path (such as \\server\folder).

The Uri constructors perform some basic cleanup of your parameters before creating a Uri, including converting the scheme and hostname to lowercase, removing default and blank port numbers, and removing the trailing slash (/). Instances of Uri have read-only properties. To modify a Uri, use a UriBuilder object.

The Uri class also provides static helper methods such as EscapeString(), which converts a string to a valid URL by converting all characters with an ASCII value greater than 127 to hexadecimal representation. The CheckHostName() and CheckSchemeName() methods accept a string and check if it is syntactically valid for the given property (although they do not attempt to determine if a host or URI exists).

The Uri class is used by many .NET types, including ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required