2014-05-13 18 views
0
www.google.com/mywork-part1/project1 
www.google.com/workspace/project1 

如何在asp.net分裂URL和替換我如何通過asp.net拆分網址「哈希」

在JavaScript中我可以這樣做

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

,但我該怎麼辦它使用asp.net 也有辦法做到像「mywork-part1」刪除「 - 」來獲取值只有「MYWORK」所以,如果有「-」然後用「空格」替換

的一個noobie SP,非常感謝

回答

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

+0

感謝您的回答,卻得到了一個錯誤「爲「string.Split的最佳重載的方法匹配(PARAMS的char [ ]''有一些無效的參數' – olo

+0

'dirs.Split('/')修復' – olo

0

簡單的方法

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

Response.Write(result);