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 }
The File
keyword is underlined in red, and the compiler shows an error:
System.Web.MVC.Controller.File(string, string, string)
is a 'method', witch is not valid in the given context.
How can I use File.Exists()
in a controller?
FileResult is the parent of all file-related action results. It is a base class that is used to send binary file content to the response. It represents an ActionResult that when executed will write a file as the response.
Also, MVC relies heavily on reflection, which allows you to inspect types at runtime using strings. Reflection is used in many programming frameworks.
You should prefix it with a namespace:
if (System.IO.File.Exists(picPath)) { //Other code }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With