在我的ASP.NET MVC應用程序中,我生成Excel報告,我有一個模板文件,我複製和更改。該模板文件放在我的解決方案中的文件夾中。我想用它如下:訪問ASP.NET MVC中的服務器上的文件
string templatePath = @"\Templates\report.xlsx";
using (var template = File.OpenRead(templatePath)) {
// Copy template and process content
}
但是這個代碼生成異常
Couldnot find a part of the path 'C:\Templates\report.xlsx'.
我應該如何引用此文件?
我也使用
string templatePath = @"~\Templates\report.xlsx";
嘗試,但導致
Could not find a part of the path 'C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\~\Templates\report.xlsx'.
它的工作但是當我使用絕對路徑,但是這是沒有意義的我的生產服務器。
如果在路徑中使用代字號,則重要的是在字符串上調用「Server.MapPath」來解析它。 – Jess 2010-11-05 15:51:10