在我的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