Uri.Segments Uri.Segments Uri.Segments Uri.Segments Property

定义

获取包含构成指定 URI 的路径段的数组。Gets an array containing the path segments that make up the specified 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[]

一个 String 数组,包含构成指定 URI 的路径段。A String array that contains the path segments that make up the specified URI.

异常

此实例代表一个相对 URI,而此属性仅对绝对 URI 有效。This instance represents a relative URI, and this property is valid only for absolute URIs.

示例

下面的示例创建Uri实例 3 段,并在屏幕上显示的段。The following example creates a Uri instance with 3 segments and displays the segments on the screen.

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]);
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 的绝对路径的字符串数组。The Segments property returns an array of strings containing the "segments" (substrings) that form the URI's absolute path. 第一个段被通过分析从其第一个字符的绝对路径,直到达到斜杠 (/) 或路径的末尾。The first segment is obtained by parsing the absolute path from its first character until you reach a slash (/) or the end of the path. 每增加一个段后前一段开始于第一个字符下, 一步斜杠或路径的末尾终止。Each additional segment begins at the first character after the preceding segment, and terminates with the next slash or the end of the path. (一个 URI 的绝对路径包含的所有内容之后的主机和端口和之前的查询和片段。)(A URI's absolute path contains everything after the host and port and before the query and fragment.)

下面的示例显示两个 Uri 的绝对路径和段。The following example shows the absolute path and segments for two URIs. 第二个示例说明了,片段和查询不是绝对路径的一部分,因而不是段。The second example illustrates that the fragment and query are not part of the absolute path and therefore are not segments.

绝对 URI:Absolute URI:
http://www.contoso.com/Chapters/Chapter1/Sections/Section1.htm

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

段:Segments:

  • /
  • 章节 /Chapters/
  • 第 1 章 /Chapter1/
  • 部分 /Sections/
  • Section1.htmSection1.htm

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

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

段:Segments:

  • /
  • 章节 /Chapters/
  • 第 1 章 /Chapter1/
  • 部分 /Sections/
  • Section1.htmSection1.htm

请注意因为的绝对路径以 / 开头,第一个段包含它和其他任何内容。Note that because the absolute path starts with a '/', the first segment contains it and nothing else.

适用于