为什么我不能在 MVC 控制器中使用 System.IO.File 方法? [英] Why can't I use System.IO.File methods in an MVC controller?
问题描述
在 MVC 控制器中使用文件之前,我试图查看它是否存在:
I am trying to see if a file exists before using it in an MVC controller:
string path = "content/image.jpg";
if (File.Exists(path))
{
//Other code
}
File
关键字有红色下划线,编译器报错:
The File
keyword is underlined in red, and the compiler shows an error:
System.Web.MVC.Controller.File(string, string, string)
是一个'method',女巫在给定的上下文中无效.
System.Web.MVC.Controller.File(string, string, string)
is a 'method', witch is not valid in the given context.
如何在控制器中使用 File.Exists()
?
How can I use File.Exists()
in a controller?
推荐答案
你应该给它加上命名空间的前缀:
You should prefix it with a namespace:
if (System.IO.File.Exists(picPath))
{
//Other code
}
这样做的原因是因为您在已经定义了 File
控制器类上的方法.
The reason for that is because you are writing this code inside a controller action which already defines a File
method on the Controller class.
这篇关于为什么我不能在 MVC 控制器中使用 System.IO.File 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!