Uri.ToString Método

Definição

Obtém uma representação canónica de cadeias para a instância especificada Uri .

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

Devoluções

A representação canónica não escapada da Uri instância. Todas as personagens não escapam, exceto #, ? e %.

Exemplos

O exemplo seguinte cria uma nova Uri instância a partir de uma cadeia. Ilustra a diferença entre o valor devolvido de OriginalString, que devolve a cadeia passada ao construtor, e de uma chamada para ToString, que retorna a forma canónica da cadeia.

// 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

Observações

A cadeia devolvida por este método não contém informação de porta quando a porta é a porta padrão do esquema.

Note

A cadeia devolvida pelo ToString método pode conter caracteres de controlo, que podem corromper o estado de uma aplicação de consola. Podes usar o GetComponents método com o UriFormat.SafeUnescaped formato para remover caracteres de controlo da string devolvida.

Aplica-se a