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


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

如何通过asp.net拆分url

在Javascript中我可以这样做

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

,但我如何使用asp.net做到这一点还有一种方法可以像"mywork-part1"那样删除"-"以获得仅"mywork"的值,因此如果有"-",则用"space"替换

a noobie of asp, Many thanks

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

便捷方式

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

最新更新