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

Definição

Obtém uma representação de cadeia de caracteres canônica para a instância Uri especificada.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

Retornos

Uma instância String que contém a representação canônica sem escape da instância Uri.A String instance that contains the unescaped canonical representation of the Uri instance. Todos os caracteres são sem escape exceto #,? e %.All characters are unescaped except #, ?, and %.

Exemplos

O exemplo a seguir cria uma Uri nova instância de uma cadeia de caracteres.The following example creates a new Uri instance from a string. Ele ilustra a diferença entre o valor retornado de OriginalString, que retorna a cadeia de caracteres que foi passada para o construtor e de uma chamada ToStringpara, que retorna a forma canônica da cadeia de caracteres.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

Comentários

A cadeia de caracteres retornada por esse método não contém informações de porta quando a porta é a porta padrão para o esquema.The string returned by this method does not contain port information when the port is the default port for the scheme.

Observação

A cadeia de caracteres retornada ToString pelo método pode conter caracteres de controle, que podem corromper o estado de um aplicativo de console.The string returned by the ToString method may contain control characters, which can corrupt the state of a console application. Você pode usar o GetComponents método com o UriFormat.SafeUnescaped formato para remover os caracteres de controle da cadeia de caracteres retornada.You can use the GetComponents method with the UriFormat.SafeUnescaped format to remove control characters from the returned string.

Segurança

SecurityPermission
para impedir que códigos parcialmente confiáveis sejam derivados de Uri.to prevent partially trusted code from deriving from Uri. Enumeração associada: Infrastructure.Associated enumeration: Infrastructure.

Aplica-se a