我創建了幾個虛擬導向器,我希望能夠從當前的http請求中獲取url。絕對URL不適用於虛擬目錄?
例如:
http://www.site.com/app_1/default.aspx ===> http://www.site.com/app_1/
http://www.site.com/app_2/default.aspx ===> http://www.site.com/app_2/
....
http://www.site.com/app_n/default.aspx ===> http://www.site.com/app_n/
我的代碼:
string urlApp = HttpContext.Current.Request.Url.AbsoluteUri.ToString();
urlApp = urlApp.Substring(0, urlApp.LastIndexOf('/') + 1);
,我試圖
string urlApp = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + "/";
在本地主機的偉大工程:http://localhost:2468/test.aspx
結果http://localhost:2468/
,但是當通過虛擬目錄http://myhost/app_1/test.aspx
結果存取http://myhost/
?我怎樣才能http://myhost/app_1/
?
你有沒有試過看看'HttpContext.Current.Request.Url.AbsoluteUri.ToString();'實際返回什麼?也許它只是返回'http:// myhost/app_1 /',然後你的子字符串是刪除app_1部分? – seekerOfKnowledge