Uri.ToString Uri.ToString Uri.ToString Uri.ToString Method

定义

获取指定的 Uri 实例的规范化字符串表示形式。Gets a canonical string representation for the specified Uri instance.

public:
 override System::String ^ ToString();
public override string ToString ();
override this.ToString : unit -> string
Public Overrides Function ToString () As String

返回

一个 String 实例,它包含 Uri 实例的非转义规范化表示形式。A String instance that contains the unescaped canonical representation of the Uri instance. 除了 #、? 和 %,所有字符均为非转义字符。All characters are unescaped except #, ?, and %.

示例

下面的示例从字符串创建Uri一个新的实例。The following example creates a new Uri instance from a string. 它阐释了从OriginalString返回的值 (返回传递到构造函数的字符串) 和对的ToString调用 (返回字符串的规范形式) 之间的差异。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 = gcnew 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 );

// The following outputs "HTTP://www.Contoso.com:80/thick%20and%20thin.htm".
Console::WriteLine( uriAddress->OriginalString );
// 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);
    ' Create a new Uri from a string address.
    Dim uriAddress As 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)

End Sub 'SampleToString

注解

当端口为方案的默认端口时, 此方法返回的字符串不包含端口信息。The string returned by this method does not contain port information when the port is the default port for the scheme.

备注

ToString方法返回的字符串可能包含控制字符, 这些字符可能会损坏控制台应用程序的状态。The string returned by the ToString method may contain control characters, which can corrupt the state of a console application. 可以将GetComponents方法UriFormat.SafeUnescaped与格式一起使用, 以从返回的字符串中删除控制字符。You can use the GetComponents method with the UriFormat.SafeUnescaped format to remove control characters from the returned string.

安全性

SecurityPermission
防止部分受信任的代码从派生Urito prevent partially trusted code from deriving from Uri. 关联枚举:InfrastructureAssociated enumeration: Infrastructure.

适用于