Compartilhar via


Uri.ToString Método

Definição

Obtém uma representação de cadeia de caracteres canônica para a instância Uri especificada.

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

Retornos

String

A representação canônica sem escape da instância Uri. Todos os caracteres são sem escape exceto #,? e %.

Exemplos

O exemplo a seguir cria uma nova Uri instância de uma cadeia de caracteres. 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 para ToString, que retorna a forma canônica da cadeia de caracteres.

// 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.
let uriAddress = 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".
printfn $"{uriAddress.ToString()}"

// The following outputs "HTTP://www.Contoso.com:80/thick%20and%20thin.htm".
printfn $"{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.

Observação

A cadeia de caracteres retornada pelo ToString método pode conter caracteres de controle, o que pode corromper o estado de um aplicativo de console. Você pode usar o GetComponents método com o UriFormat.SafeUnescaped formato para remover caracteres de controle da cadeia de caracteres retornada.

Aplica-se a