2012-11-07 153 views
4

的絕對路徑,我呼籲在C#中的方法如下:獲取相對路徑

return Chart.RenderChartHTML("../../Charts/MSLine.swf"); 

的事情是,該路徑可以在哪個文件夾我從打電話RenderChartHTML不同,這取決於。

我嘗試以下,這樣找到的絕對路徑,但不工作:

string mslinepath = HttpContext.Current.Server.MapPath("~/Charts/MSLine.swf"); 

return Chart.RenderChartHTML(mslinepath); 
+0

這有幫助嗎? http://stackoverflow.com/questions/837488/how-can-i-get-the-applications-path-in-net-in-a-console-app – Joe

回答

8

使用ResolveUrl()。它將URL轉換爲請求客戶端可用的URL。

那麼試試這個:

string mslinepath = ResolveUrl("~/Charts/MSLine.swf") 

希望這將幫助!

+2

謝謝。爲什麼我需要〜雖然? –

+2

「〜」表示從「根」目錄和「。」獲取相對路徑。指的是從當前目錄獲取相對路徑。 –

7

你不需要~/。只需HttpContext.Current.Server.MapPath("Charts/MSLine.swf");