This topic has not yet been rated - Rate this topic

Uri.ToString Method

Gets a canonical string representation for the specified Uri instance.

Namespace:  System
Assembly:  System (in System.dll)
[SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags = SecurityPermissionFlag.Infrastructure)]
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 %.

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

Note Note

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.

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.
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 "http://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);


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
He's right - And UriBuilder can also lead to unintended consequences

Image you create a hyperlink using UriBuilder.

You then pass it into a method that takes a Uri using builder.Uri property.

If this method then uses OriginalString, as it should do, you will end up with the port number in the string representation.

This can cause problems with OAuth/Facebook, because it doesn't like the port number part (as it doesn't match the callback URL you setup when registering your app with FB).

Uri.ToString() Will Corrupt Escaped Parameters

As mentioned in the documentation, this method returns the "unescaped canonical representation" of the URI. This means that characters that should remain escaped will become unescaped. This can corrupt the URL.

For example, a search for "X&Y" is "http://www.bing.com/search?q=X%26Y" but new Uri("http://www.bing.com/search?q=X%26Y").ToString() will return http://www.bing.com/search?q=X&Y, which is incorrect.

There is no satisfactory workaround for this bug.  Partial workarounds include:

  • Use the Uri.AbsoluteUri property instead.  This property retains correct escaping, but will fail if the object contains a relative URL rather than an absolute one.
  • Use the Uri.OriginalString property instead for relative URL's.  However, because this returns the original string that was passed to the constructor, it will not reflect any changes you have made to the URL using the Uri object's properties.

For more information, see http://code.logos.com/blog/2010/08/uritostring_must_die.html.