2016-02-23 46 views
2

我需要獲取ASP.NET-MVC-5應用程序的Base-/Application-Path。ApplicationPath vs「〜」,哪個更適合找到應用程序路徑?

我發現了兩個機會得到它,想知道什麼是更好的選擇:

UrlHelper.Content("~") 

UrlHelper.RequestContext.HttpRequest.Request.ApplicationPath 
+0

我建議很少有在裏面。 ApplicationPath可能稍快一點(它已經在上游進行了進一步計算),因爲助手需要計算它(在某個級別上)。儘管如此,這可能是微觀優化。 – Liam

回答

0

使用此,我認爲這是更好的

Server.MapPath("~/YourFolder/"); 

這裏「〜」代表您的根文件夾

即。如果你想從「目錄」文件夾

「pic1.gif」文件

只需使用

string _path = Server.MapPath("~/Contents/"); 
string _myFile = "pic1.gif"; 
string _fullPath = _path + _myFile; 
+4

問題是*想知道什麼是更好的選擇*。你還沒有回答這個問題 – Liam

+0

這根本不提供應用程序路徑。 –

相關問題