Uri.ToString Méthode

Définition

Obtient une représentation sous forme de chaîne canonique pour l’instance spécifiée Uri .

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

Retours

Représentation canonique non boucrée de l’instance Uri . Tous les caractères ne sont pas bouctés à l’exception de #, ?, et %.

Exemples

L’exemple suivant crée une Uri instance à partir d’une chaîne. Il illustre la différence entre la valeur retournée par OriginalString, qui retourne la chaîne qui a été passée au constructeur, et d’un appel à ToString, qui retourne la forme canonique de la chaîne.

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

Remarques

La chaîne retournée par cette méthode ne contient pas d’informations de port lorsque le port est le port par défaut du schéma.

Note

La chaîne retournée par la ToString méthode peut contenir des caractères de contrôle, qui peuvent endommager l’état d’une application console. Vous pouvez utiliser la méthode avec le GetComponentsUriFormat.SafeUnescaped format pour supprimer les caractères de contrôle de la chaîne retournée.

S’applique à