Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在URL中的第一个斜杠之后获取任何内容_C#_Url_Slash - Fatal编程技术网

C# 在URL中的第一个斜杠之后获取任何内容

C# 在URL中的第一个斜杠之后获取任何内容,c#,url,slash,C#,Url,Slash,我可以使用以下方式获取浏览器url:string url=HttpContext.Current.Request.url.AbsoluteUri 但是,如果我有一个如下url: http://www.test.com/MyDirectory/AnotherDir/testpage.aspx 如何获取其中的“MyDirectory”部分,在.NET中是否有实用程序来获取该部分,或者是否需要进行字符串操作 如果我进行字符串操作并在“/”的第一个实例之后说任何话,那么它不会在http:”之后返回斜杠

我可以使用以下方式获取浏览器url:
string url=HttpContext.Current.Request.url.AbsoluteUri
但是,如果我有一个如下url:

http://www.test.com/MyDirectory/AnotherDir/testpage.aspx
如何获取其中的“MyDirectory”部分,在.NET中是否有实用程序来获取该部分,或者是否需要进行字符串操作

如果我进行字符串操作并在“/”的第一个实例之后说任何话,那么它不会在http:”之后返回斜杠吗?如果我的url是
www.test.com/MyDirectory/AnotherDir/testpage.aspx


有人能帮忙吗

你可以通过
路径和查询
Url的属性来获取

var path = HttpContext.Current.Request.Url.PathAndQuery;

它将返回
/MyDirectory/AnotherDir/testpage.aspx
从url实例化Uri实例:

Uri myUri = new Uri("http://www.test.com/MyDirectory/AnotherDir/testpage.aspx");
然后,可以使用以下方法将路径段放入字符串数组:

string[] segments = myUri.Segments
您的第一个“MyDirectory”文件夹将位于:

string myFolderName = segments[0];

您是否检查了该类的其他属性,例如?谢谢,这正是我所需要的。
 Uri uriAddr = new Uri("http://www.test.com/MyDirectory/AnotherDir/testpage.aspx");
 var firstSegment= uriAddress.Segments.Where(seg => seg != "/").First();