Język

Uri.ToString Metoda

Definicja

Pobiera reprezentację ciągu kanonicznego dla określonego Uri wystąpienia.

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

Zwraca

Unescaped kanoniczna reprezentacja Uri wystąpienia. Wszystkie znaki są niewyobrażalne z wyjątkiem znaków #, ?, i %.

Przykłady

Poniższy przykład tworzy nowe Uri wystąpienie na podstawie ciągu. Ilustruje różnicę między wartością zwróconą z OriginalString, która zwraca ciąg przekazany do konstruktora, a wywołaniem metody , ToStringktóra zwraca kanoniczną formę ciągu.

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

Uwagi

Ciąg zwracany przez tę metodę nie zawiera informacji o porcie, gdy port jest domyślnym portem schematu.

Note

Ciąg zwracany przez metodę ToString może zawierać znaki sterujące, które mogą uszkodzić stan aplikacji konsolowej. Możesz użyć GetComponents metody z formatem UriFormat.SafeUnescaped , aby usunąć znaki sterujące z zwróconego ciągu.

Dotyczy