如何获得没有root的绝对URL?

mag*_*gie 1 .net c# asp.net uri

我有这个根:http:// localhost/foldername/about

我想只得到那个/about部分.

我怎样才能做到这一点?

Cha*_*thJ 9

使用Uri.Segments属性

Segments属性返回一个字符串数组,其中包含构成URI绝对路径的"segments"(子字符串).第一个段是通过解析第一个字符的绝对路径直到达到斜杠(/)或路径的结尾来获得的.每个附加段从前一段之后的第一个字符开始,并以下一个斜杠或路径末尾结束.(URI的绝对路径包含主机和端口之后以及查询和片段之前的所有内容.)

Uri uriAddress1 = new Uri("http://localhost/foldername/about");
Console.WriteLine("The parts are {0}, {1}, {2}", uriAddress1.Segments[0], 
                  uriAddress1.Segments[1], uriAddress1.Segments[2]);
Run Code Online (Sandbox Code Playgroud)

了解有关Uri Class MSDN的更多信息