Freigeben über


Uri.ToString Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets a canonical string representation for the specified Uri instance.

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

Syntax

'Declaration
Public Overrides Function ToString As String
public override string ToString()

Return Value

Type: System.String
A String instance that contains the unescaped canonical representation of the Uri instance. All characters are unescaped except #, ?, and %.

Remarks

The string returned by this method does not contain port information when the port is the default port for the scheme.

NoteNote:

The string returned by the ToString method may contain control characters, which can corrupt the state of a console application. You can use the GetComponents method with the UriFormat.SafeUnescaped format to remove control characters from the returned string.

Examples

The following example creates a new Uri instance from a string. It illustrates the difference between the value returned from OriginalString, which returns the string that was passed to the constructor, and from a call to ToString, which returns the canonical form of the string.

' Create a new Uri from a string address.
Dim uriAddress As Uri = New Uri("HTTP://www.Contoso.com:80/thick%20and%20thin.htm")

' Write the new Uri to the console and note the difference in the two values.
' ToString() gives the canonical version.  OriginalString gives the orginal 
' string that was passed to the constructor.

' The following outputs "https://www.contoso.com/thick and thin.htm".
outputBlock.Text &= uriAddress.ToString()
outputBlock.Text &= vbCrLf

' The following outputs "HTTP://www.Contoso.com:80/thick%20and%20thin.htm".
outputBlock.Text += uriAddress.OriginalString
outputBlock.Text &= vbCrLf
// Create a new Uri from a string address.
Uri uriAddress = new Uri("HTTP://www.Contoso.com:80/thick%20and%20thin.htm");

// Write the new Uri to the console and note the difference in the two values.
// ToString() gives the canonical version.  OriginalString gives the orginal 
// string that was passed to the constructor.

// The following outputs "https://www.contoso.com/thick and thin.htm".
outputBlock.Text += uriAddress.ToString();
outputBlock.Text += "\n";

// The following outputs "HTTP://www.Contoso.com:80/thick%20and%20thin.htm".
outputBlock.Text += uriAddress.OriginalString;
outputBlock.Text += "\n";
// Create a new Uri from a string address.
Uri uriAddress = new Uri("HTTP://www.Contoso.com:80//thick%20and%20thin.htm");

// Write the new Uri to the console and note the difference in the two values.
// ToString() gives the canonical version.  OriginalString gives the orginal 
// string that was passed to the constructor.

// The following outputs "https://www.contoso.com/thick and thin.htm".
Console.WriteLine(uriAddress.ToString()); 

// The following outputs "HTTP://www.Contoso.com:80//thick%20and%20thin.htm".
Console.WriteLine(uriAddress.OriginalString);

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

See Also

Reference