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/
  • Chapter1.txtChapter1/
  • 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/
  • Chapter1.txtChapter1/
  • Sections/
  • Section1 .htmSection1.htm

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

适用于