Uri.GetLeftPart(UriPartial) Metod

Definition

Hämtar den angivna delen av en Uri instans.

public:
 System::String ^ GetLeftPart(UriPartial part);
public string GetLeftPart(UriPartial part);
member this.GetLeftPart : UriPartial -> string
Public Function GetLeftPart (part As UriPartial) As String

Parametrar

part
UriPartial

Ett av uppräkningsvärdena som anger slutet på den URI-del som ska returneras.

Returer

Den angivna delen av instansen Uri .

Undantag

Den aktuella Uri instansen är inte en absolut instans.

Angivet part är inte giltigt.

Exempel

I följande exempel skapas en Uri instans och sökvägen till konsolen skrivs.

// Create Uri
Uri uriAddress = new Uri("http://www.contoso.com/index.htm#search");
Console.WriteLine(uriAddress.Fragment);
Console.WriteLine("Uri {0} the default port ", uriAddress.IsDefaultPort ? "uses" : "does not use");

Console.WriteLine("The path of this Uri is {0}", uriAddress.GetLeftPart(UriPartial.Path));
Console.WriteLine("Hash code {0}", uriAddress.GetHashCode());
// The example displays output similar to the following:
//        #search
//        Uri uses the default port
//        The path of this Uri is http://www.contoso.com/index.htm
//        Hash code -988419291
// Create Uri
let uriAddress = Uri "http://www.contoso.com/index.htm#search"
printfn $"{uriAddress.Fragment}"
printfn $"""Uri {if uriAddress.IsDefaultPort then "uses" else "does not use"} the default port """

printfn $"The path of this Uri is {uriAddress.GetLeftPart UriPartial.Path}"
printfn $"Hash code {uriAddress.GetHashCode()}"
// The example displays output similar to the following:
//        #search
//        Uri uses the default port
//        The path of this Uri is http://www.contoso.com/index.htm
//        Hash code -988419291
' Create Uri
Dim uriAddress As New Uri("http://www.contoso.com/index.htm#search")
Console.WriteLine(uriAddress.Fragment)
Console.WriteLine("Uri {0} the default port ", If(uriAddress.IsDefaultPort, "uses", "does not use")) 

Console.WriteLine("The path of this Uri is {0}", uriAddress.GetLeftPart(UriPartial.Path))
Console.WriteLine("Hash code {0}", uriAddress.GetHashCode())
' The example displays output similar to the following:
'        #search
'        Uri uses the default port
'        The path of this Uri is http://www.contoso.com/index.htm
'        Hash code -988419291

Kommentarer

Metoden GetLeftPart returnerar en sträng som innehåller den vänstra delen av URI-strängen och slutar med den del som anges av part.

Important

Metoden GetLeftPart utför Unicode-teckenkodning och normalisering som en del av bearbetningen. Det är inte en enkel strängmanipuleringsmetod. Den returnerade strängen kan skilja sig från den ursprungliga URI-strängen på grund av det här kodningsbeteendet.

Metoden GetLeftPart motsvarar anrop GetComponents med lämpliga UriComponents flaggor. Ett exempel:

  • GetLeftPart(UriPartial.Authority) är motsvarande GetComponents(UriComponents.Scheme | UriComponents.UserInfo | UriComponents.Host | UriComponents.Port, UriFormat.UriEscaped)

GetLeftPart innehåller avgränsare i följande fall:

  • Scheme innehåller schemats avgränsare.
  • Authority innehåller inte sökvägsgränsaren.
  • Path innehåller sökvägsavgränsaren och eventuella avgränsare i den ursprungliga URI:n upp till fråge- eller fragmentavgränsaren.
  • Query Pathinnehåller , plus frågan och dess avgränsare.

I följande exempel visas en URI och resultatet av att anropa GetLeftPart med Scheme, Authority, Patheller Query.

URI Scheme Auktoritet Sökväg Query
http://www.contoso.com/index.htm?date=today http:// http://www.contoso.com http://www.contoso.com/index.htm http://www.contoso.com/index.htm?date=today
http://www.contoso.com/index.htm#main http:// http://www.contoso.com http://www.contoso.com/index.htm http://www.contoso.com/index.htm
mailto:user@contoso.com?subject=uri mailto: <none> mailto:user@contoso.com mailto:user@contoso.com?subject=uri
nntp://news.contoso.com/123456@contoso.com nntp:// nntp://news.contoso.com nntp://news.contoso.com/123456@contoso.com nntp://news.contoso.com/123456@contoso.com
news:123456@contoso.com news: <none> news:123456@contoso.com news:123456@contoso.com
file://server/filename.ext file:// file://server file://server/filename.ext file://server/filename.ext

Gäller för