共用方式為


Uri.Segments 屬性

定義

會得到一個陣列,包含構成指定 URI 的路徑段。

public:
 property cli::array <System::String ^> ^ Segments { cli::array <System::String ^> ^ get(); };
public string[] Segments { get; }
member this.Segments : string[]
Public ReadOnly Property Segments As String()

屬性值

String[]

構成指定 URI 的路徑段。

例外狀況

此實例代表一個相對 URI,且此性質僅適用於絕對 URI。

範例

以下範例 Uri 建立一個包含 3 個區段的實例,並在螢幕上顯示這些區段。

Uri uriAddress1 = new Uri("http://www.contoso.com/title/index.htm");
Console.WriteLine("The parts are {0}, {1}, {2}", uriAddress1.Segments[0], uriAddress1.Segments[1], uriAddress1.Segments[2]);
let uriAddress1 = Uri "http://www.contoso.com/title/index.htm"
printfn $"The parts are {uriAddress1.Segments[0]}, {uriAddress1.Segments[1]}, {uriAddress1.Segments[2]}"
Dim uriAddress1 As New Uri("http://www.contoso.com/title/index.htm")
Console.WriteLine("The parts are {0}, {1}, {2}", uriAddress1.Segments(0), uriAddress1.Segments(1), uriAddress1.Segments(2))

備註

Segments 特性回傳一個包含構成 URI 絕對路徑的「段」(子字串)字串陣列。 第一段是從第一個字元解析絕對路徑,直到到達斜線(/)或路徑的終點。 每個額外段落從前一段後的第一個字元開始,並以下一個斜線或路徑的結尾結束。 (URI 的絕對路徑包含主機與埠之後,以及查詢與片段之前的所有內容。)

以下範例展示了兩個 URI 的絕對路徑與段數。 第二個例子說明片段與查詢不屬於絕對路徑,因此不是段。

絕對的URI: http://www.contoso.com/Chapters/Chapter1/Sections/Section1.htm

絕對路線:/章節/第一章/章節/Section1.htm

單元:

  • /
  • 章節/
  • 第一章/
  • 章節/
  • Section1.htm

絕對的URI: http://www.contoso.com/Chapters/Chapter1/Sections/Section1.htm#page1?answer=NO

絕對路線:/章節/第一章/章節/Section1.htm

單元:

  • /
  • 章節/
  • 第一章/
  • 章節/
  • Section1.htm

請注意,由於絕對路徑以 '/' 開頭,第一段包含該路徑,且不包含其他。

適用於