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

段:

  • /
  • 章节/
  • 第 1 章/
  • 节/
  • Section1.htm

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

绝对路径:/Chapters/Chapter1/Sections/Section1.htm

段:

  • /
  • 章节/
  • 第 1 章/
  • 节/
  • Section1.htm

请注意,由于绝对路径以“/”开头,因此第一个段包含它,而不包含其他任何内容。

适用于