2012-11-07 34 views
0

我有以下路徑:如何操作c#asp.net中的URL路徑?

-http://localhost/portal/reportview.aspx

,但我需要檢索的第一個3個部分:

-http :// localhost/portal/

我試過HttpContext.Current.Request.Url.GetLeftPart();沒有運氣。

+3

如果放在Request.Url調試中斷,你可以看到,可以給你的所有物品,像'.Path'和可以做的工作。比提出問題要快。 – Aristos

+0

這個問題可能對您有興趣 http://stackoverflow.com/questions/4630249/get-url-without-querystring –

+0

總是前三部分或刪除「reportview.aspx」?或者以不同的方式,如果url是'http:// localhost/portal/sub/page.aspx',怎麼辦? –

回答

1

試試這個;

string s = "http://localhost/portal/reportview.aspx"; 
string y = string.Format("{0}//{2}/{3}/",s.Split('/')); 
+0

Split在'//'之間找到一個空字符串,所以要麼移動索引,要麼在Split中使用StringSplitOptions.RemoveEmptyEntries。 –

+0

現在正在通過刪除{1}來糾正。謝謝 – Kaf

1

您可以使用子字符串。

string str = "http://localhost/portal/reportview.aspx"; 

string left = str.Substring(0, str.LastIndexOf("/"));