Uri.ToString Metod

Definition

Hämtar en kanonisk strängrepresentation för den angivna Uri instansen.

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

Returer

Den ej kapslade kanoniska representationen av instansen Uri . Alla tecken är ej kapslade utom #, ?och %.

Exempel

I följande exempel skapas en ny Uri instans från en sträng. Den illustrerar skillnaden mellan det värde som returneras från OriginalString, som returnerar strängen som skickades till konstruktorn och från ett anrop till ToString, som returnerar strängens kanoniska form.

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

Kommentarer

Strängen som returneras med den här metoden innehåller inte portinformation när porten är standardporten för schemat.

Note

Strängen ToString som returneras av metoden kan innehålla kontrolltecken, vilket kan skada tillståndet för ett konsolprogram. Du kan använda GetComponents metoden med UriFormat.SafeUnescaped formatet för att ta bort kontrolltecken från den returnerade strängen.

Gäller för