2012-01-23 99 views
0

我刮URL的一個網站,並在錨標籤都有自己href的值正好被設置爲查詢字符串如..Uri.TryCreate不會產生結果,我預計

<a href="?AppId=12345&CatId=13">Details</a> 

當前的URL頁面是這樣的..

http://www.theurl.com/ThePage.aspx?PageNo=2 

所以我在尋找的URL將被

http://www.theurl.com/ThePage.aspx?AppId=12345&CatId=13 

戈噸此我使用的方法Uri.TryCreate,所以我傳遞以下參數(第一兩個參數是類型的URI和不字符串)..

Uri.TryCreate("http://www.theurl.com/ThePage.aspx?PageNo=2", "?AppId=12345&CatId=13", out uri); 

然而out參數「URI」正在設置爲...

http://www.theurl.com/?AppId=12345&CatId=13 

正如您所看到的,它將刪除.aspx路徑。你能推薦一個更好的方法來解決這個問題,或者解釋爲什麼它不能像我認爲的那樣工作?

回答

1

嘗試這種情況:

Uri.TryCreate("http://www.theurl.com/ThePage.aspx?PageNo=2", "ThePage.aspx?AppId=12345&CatId=13", out uri); 

根據the docs,第一種是基本URI,第二個是相對URI。

0

嗯。我不確定發生的行爲是否正確。這可能是一個錯誤。

在任何情況下,你可能會發現下面的更加得心應手:

UriBuilder uBuild = new UriBuilder("http://www.theurl.com/path/thePage.aspx?PageNo=2"); 
uBuild.Query = "AppId=12345&CatId=13"; 
Uri newUri = ub.Uri;//http://www.theurl.com/path/thePage.aspx?AppId=12345&CatId=13 
//Note that we can reuse uBuild as we continue to parse the page, as long as we're only dealing with cases where only the query changes. 
uBuild.Query = "AppId=678&CatId=2"; 
Uri anotherUri = ub.Uri;//http://www.theurl.com/path/thePage.aspx?AppId=678&CatId=2