如何从url获取特定目录名称 [英] How can I get a particular directory name from url
本文介绍了如何从url获取特定目录名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想提取URL的特定目录的名称,并将其保存到ASP.NET C#中服务器端的字符串中。
例如,假设我有一个如下所示的网址:
http:// localhost:1096 / directory1 / directory2 / default.aspx
如何从URL获取directory1的值并将其保存为字符串变量?
提前致谢。
I want to extract the name of a particular directory of a URL and save it to a string from the server-side in ASP.NET C#.
For example, lets say I have a URL that looks like this:
http://localhost:1096/directory1/directory2/default.aspx
How would I get the value of directory1 from the URL and save it to a string variable?
Thanks in advance.
推荐答案
string requestString = Request.Url.GetComponents(UriComponents.Path, UriFormat.Unescaped);
string[] directories = requestString.Split(new char[] { '/' });
string firstSubDirectory = directories[1];
string secondSubDirectory = directories[2];
//etc...
我在下一页中得到了解决方案:
http://msdn.microsoft.com/en-us/library/system.uri.segments [ ^ ]
I got the solution in the following page:
http://msdn.microsoft.com/en-us/library/system.uri.segments[^]
这篇关于如何从url获取特定目录名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文