Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在ASP.NET MVC中访问服务器上的文件_Asp.net_Asp.net Mvc_Asp.net Mvc 2_File_File Io - Fatal编程技术网

在ASP.NET MVC中访问服务器上的文件

在ASP.NET MVC中访问服务器上的文件,asp.net,asp.net-mvc,asp.net-mvc-2,file,file-io,Asp.net,Asp.net Mvc,Asp.net Mvc 2,File,File Io,在我的ASP.NET MVC应用程序中,我正在生成excel报告,我有一个复制和更改的模板文件。此模板文件放在“我的解决方案”的文件夹中。我想按如下方式使用它: string templatePath = @"\Templates\report.xlsx"; using (var template = File.OpenRead(templatePath)) { // Copy template and process content } 但这段代码会生成一个异常 Couldnot f

在我的ASP.NET MVC应用程序中,我正在生成excel报告,我有一个复制和更改的模板文件。此模板文件放在“我的解决方案”的文件夹中。我想按如下方式使用它:

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'.

但是,当我使用绝对路径时,它确实可以工作,但这对我的生产服务器来说毫无意义。

我相信您会使用普通的ASP.NET方式,假设模板是web应用程序中的目录

string templatePath = @"~\Templates\report.xlsx";

using (var template = File.OpenRead(Server.MapPath(templatePath))) {
  // Copy template and process content
}

您需要使用Server.MapPath(path)


这将把虚拟目录路径映射到服务器上的完整路径。

如果在路径中使用波浪号,则在字符串上调用
server.MapPath
来解析它是很重要的。这样试试
server.MapPath(“/Templates/report.xlsx”)
?坚持你刚刚删除了你的评论,说它不起作用。是吗?一开始我没有很好地阅读他的答案。他已将其放入
文件.OpenRead()
中。在我看来,把这句话写在作业上是更简洁的代码,但这是我个人的风格。反斜杠对我来说很有用,你能解释一下为什么正斜杠更好吗?我想我从来没有试过用反斜杠。反斜杠通常表示实际的物理路径,而正斜杠表示容纳应用程序的虚拟目录。Server.MapPath()获取同一服务器上的虚拟目录,并将其映射到其完全限定的物理路径。也许知道你打算做什么是明智之举,这就是反斜杠起作用的原因。我不完全确定。我打赌我知道它为什么有效。Templates\report.xlsx是根虚拟目录的物理子目录,因此可以正常工作。但是,我敢打赌,如果Templates目录只是根虚拟目录的一个虚拟子目录,而物理目录在其他地方,那么反斜杠将不起作用。我应该找个时间测试一下。如果是这样的话,我仍然建议使用前向斜杠,以防在保持虚拟目录不变的同时需要移动目录结构。。
string templatePath = Server.MapPath("~/Templates/report.xlsx"); //Note the forward slashes instead of backslashes.

using (var template = File.OpenRead(templatePath)) {
  // Copy template and process content
}