2016-01-05 98 views
0

我想寫使用asp.net的MVC ..however文件JS的東西,它似乎不工作寫文件JS路徑工程腳本

new System.IO.StreamWriter("Assets/data.js", true)) 
     { 
      file.WriteLine("xyz"); 
     } 

它顯示

error at "Assets/data.js". Could not find a part of the path 'C:\Program Files (x86)\IIS Express\~\Assets\data.js'. 

回答

0

既然你說使用ASP.NET MVC你應該使用@url.Content()輔助方法,你的虛擬路徑映射到絕對路徑一樣

new System.IO.StreamWriter(url.Content("~/Assets/data.js", true)) 
     { 
      file.WriteLine("xyz"); 
     } 

如果您正在使用ASP.NET則使用Server.MapPath(),它會執行相同的操作。

+0

謝謝。你的回答很有用 – john

0

您應該使用Server.MapPath()方法獲取到您的位置的路徑並使用它。

var p = Server.MapPath("~/Assets/data.js"); 
using(var file= new System.IO.StreamWriter(p, true)) 
{ 
    file.WriteLine("xyz"); 
} 

Server.MapPath("~")會給你的路徑,其中執行代碼的應用程序的根。由於我們指定了更多路徑位置,因此您將在您的應用程序根目錄下獲得Assets/data.js的正確路徑。

+0

謝謝。你的答案很有用 – john

相關問題