Export (0) Print
Expand All
9 out of 16 rated this helpful - Rate this topic

Uri Class

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

For a list of all members of this type, see Uri Members.

System.Object
   System.MarshalByRefObject
      System.Uri

[Visual Basic]
<Serializable>
Public Class Uri
   Inherits MarshalByRefObject
   Implements ISerializable
[C#]
[Serializable]
public class Uri : MarshalByRefObject, ISerializable
[C++]
[Serializable]
public __gc class Uri : public MarshalByRefObject, ISerializable
[JScript]
public
   Serializable
class Uri extends MarshalByRefObject implements
   ISerializable

Thread Safety

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

Remarks

A URI is a compact representation of a resource available to your application on the 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.

The Uri class stores only absolute URIs (for example, "http://www.contoso.com/index.htm"). 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 will not escape URI strings if the string is a well-formed URI including a scheme identifier that contains escape sequences. The constructors will escape relative URI strings that contain a percent sign (%).

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

  • Converts the URI scheme to lower case.
  • Converts the host name to lower case.
  • Removes default and empty port numbers.
  • Simplifies the URI by removing superfluous segments such as "/" and "/test" segments.

You can transform the contents of the Uri class from an escape encoded URI reference to a readable URI reference using the ToString method.

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

Note   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].

.NET Compact Framework Platform Note:  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.

Example

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

[Visual Basic] 
Dim siteUri As New Uri("http://www.contoso.com/")
        
Dim wr As WebRequest = WebRequest.Create(siteUri)


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


[C++] 
Uri* siteUri = new Uri(S"http://www.contoso.com/");
 
WebRequest* wr = WebRequest::Create(siteUri);


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

Requirements

Namespace: System

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: System (in System.dll)

See Also

Uri Members | System Namespace | UriBuilder

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.