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。

範例

下列範例會建立具有 3 個 Uri 區段的實例,並在畫面上顯示區段。

Uri^ uriAddress1 = gcnew 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 ] );
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 的絕對路徑包含主機和埠之後和查詢和 fragment.)

下列範例顯示兩個 URI 的絕對路徑和區段。 第二個範例說明片段和查詢不是絕對路徑的一部分,因此不是區段。

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

絕對路徑:/Chapters/Chapter1/Sections/Section1.htm

段:

  • /
  • 章節/
  • Chapter1/
  • 區段/
  • Section1.htm

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

絕對路徑:/Chapters/Chapter1/Sections/Section1.htm

段:

  • /
  • 章節/
  • Chapter1/
  • 區段/
  • Section1.htm

請注意,因為絕對路徑以 '/' 開頭,所以第一個區段包含它,而且沒有其他專案。

適用於