Export (0) Print
Expand All

Uri Class

Provides an object representation of a uniform resource identifier (URI) and easy access to the parts of the URI.

Namespace: System
Assembly: System (in system.dll)

[SerializableAttribute] 
public class Uri : ISerializable
/** @attribute SerializableAttribute() */ 
public class Uri implements ISerializable
SerializableAttribute 
public class Uri implements ISerializable

A URI is a compact representation of a resource available to your application on the intranet or Internet. The Uri class defines the properties and methods for handling URIs, including parsing, comparing, and combining. The Uri class properties are read-only; to create a modifiable object, use the UriBuilder class.

Relative URIs (for example, "/new/index.htm") must be expanded with respect to a base URI so that they are absolute. The MakeRelative method is provided to convert absolute URIs to relative URIs when necessary.

The Uri constructors do not escape URI strings if the string is a well-formed URI including a scheme identifier.

The Uri properties return a canonical data representation in escaped encoding, with all characters with Unicode values greater than 127 replaced with their hexadecimal equivalents. To put the URI in canonical form, the Uri constructor performs the following steps:

  • Converts the URI scheme to lowercase.

  • Converts the host name to lowercase.

  • If the host name is an IPv6 address, the canonical IPv6 address is used. ScopeId and other optional IPv6 data are removed.

  • Removes default and empty port numbers.

  • Canonicalizes the path for hierarchical URIs by compacting sequences such as /./, /../, //, including escaped representations.

  • For hierarchical URIs, if the host is not terminated with a forward slash (/), one is added.

  • Any reserved characters in the URI are escaped in accordance with RFC 2396.

You can transform the contents of the Uri class from an escape encoded URI reference to a readable URI reference by using the ToString method. Note that some reserved characters might still be escaped in the output of the ToString method. This is to support unambiguous reconstruction of a URI from the value returned by ToString.

Some URIs include a fragment identifier or a query or both. A fragment identifier is any text that follows a number sign (#), not including the number sign; the fragment text is stored in the Fragment property. Query information is any text that follows a question mark (?) in the URI; the query text is stored in the Query property.

In the .NET Framework version 1.1, if the string specified to a constructor contains an unknown scheme and "c:\", the Uri class inserts "//" after the colon. For example, the URI xyz:c:\abc is converted to xyz://c:/abc. In the .NET Framework version 2.0, this behavior has been removed, and the example string is converted to xyz:c:/abc.

NoteNote

The URI class supports the use of IP addresses in both quad-notation for IPv4 protocol and colon-hexadecimal for IPv6 protocol. Remember to enclose the IPv6 address in square brackets, as in http://[::1].

Performance Considerations

If you use a Web.config file that contains URIs to initialize your application, additional time is required to process the URIs if their scheme identifiers are nonstandard. In such a case, initialize the affected parts of your application when the URIs are needed, not at start time.

Notes to Callers Because of security concerns, your application should use caution when accepting Uri instances from untrusted sources and with dontEscape set to true.You can check a URI string for validity by calling the IsWellFormedOriginalString method.

Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows CE Platform Note: The .NET Compact Framework does not differentiate between relative and absolute paths. Also, the .NET Compact Framework processes URI strings prefixed by the file:// scheme differently from the full .NET Framework. A relative file://myfile specification resolves as \\myfile. Using file:///myfile (three slashes) resolves as \myfile in the root directory. To ensure successful operations, specify absolute path information.

The following example creates an instance of the Uri class and uses it to create a WebRequest instance.

Uri siteUri = new Uri("http://www.contoso.com/");
 
WebRequest wr = WebRequest.Create(siteUri);


Uri siteUri = new Uri("http://www.contoso.com/");
WebRequest wr = WebRequest.Create(siteUri);

var siteUri : Uri = new Uri("http://www.contoso.com/");
var wr : WebRequest = WebRequest.Create(siteUri);


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft