Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I use System.IO.File methods in an MVC controller?

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?

like image 564
Pomster Avatar asked Apr 24 '13 09:04

Pomster


People also ask

What is FileResult in MVC?

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.

How does MVC know which controller to use?

Also, MVC relies heavily on reflection, which allows you to inspect types at runtime using strings. Reflection is used in many programming frameworks.


1 Answers

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.

like image 170
Darin Dimitrov Avatar answered Oct 15 '22 05:10

Darin Dimitrov