閱讀英文

共用方式為


Uri.GetLeftPart(UriPartial) 方法

定義

取得 Uri 執行個體的指定部分。

C#
public string GetLeftPart(UriPartial part);

參數

part
UriPartial

其中一個列舉值,其指定要傳回之 URI 部分的結尾。

傳回

Uri 執行個體的指定部分。

例外狀況

目前的 Uri 執行個體不是絕對執行個體。

指定的 part 無效。

範例

下列範例會 Uri 建立 實例,並將路徑寫入主控台。

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

備註

方法 GetLeftPart 會傳回包含 URI 字串最左邊部分的字串,結尾為 所 part 指定的部分。

GetLeftPart 在下列情況下包含分隔符號:

  • Scheme 包含配置分隔符號。
  • Authority 不包含路徑分隔符號。
  • Path 包含原始 URI 中路徑分隔符號和任何分隔符號,以及查詢或片段分隔符號為止。
  • Query Path包含 ,加上查詢及其分隔符號。

下列範例示範使用 、 AuthorityPathQuery 呼叫 GetLeftPartScheme 的 URI 和結果。

URI 配置 授權單位 路徑 查詢
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

適用於

產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1