如何在asp.net中拆分url并替换" hash"

时间:2014-05-13 02:44:14

标签: asp.net

www.google.com/mywork-part1/project1
www.google.com/workspace/project1

如何通过asp.net分割网址

在Javascript中,我可以这样做

var getURL = 'www.google.com/mywork-part1/project1';
var newURL =  getURL.split("/")[2];

但我如何使用asp.net做到这一点 还有一种方法可以像“mywork-part1”一样删除“ - ”以仅获取值“mywork”,如果有“-”则替换为“space”

asp的noobie,非常感谢

2 个答案:

答案 0 :(得分:1)

Uri uri = new Uri("http://www.myDomain.com/someDir/anotherDir");
string dirs = uri.GetComponents(UriComponents.Path, UriFormat.SafeUnescaped);
string[] dirArray = dirs.Split("/");
for (int i = 0; i <= dirArray.Length - 1; i++) {
    Trace.Warn(string.Format("Directory {0} is at index {1}", dirArray[i].Replace("-",String.Empty), i.ToString()));
    // find each directory in the path with its corresponding index number '
}
    // reference someDir directly '
Trace.Warn(dirArray[0]);

参考:http://www.codepal.co.uk/show/Using_RequestUrl_to_find_specific_parts_of_the_web_pages_address

答案 1 :(得分:0)

简单方法

string Url = "www.google.com/mywork-part1/project1";
string[] newURL = Url.Split('/');
string result = newURL[1].Replace("-", " ");

Response.Write(result);