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 some ...

Get C# in a Nutshell, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.