Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In MVC 2, How would you determine a file exists at the server using C#?

I know you can do this:

if( System.IO.File.Exists(
    @"C:\INetPub\MVCWebsite\Content\Images\image.jpg") ) { ... }

and you can do this to reference files in MVC:

Url.Content("~/Content/Images/image.jpg")

So is there a way to relatively check that "~/Content/Images/image.jpg" exists (in MVC?)?

like image 305
Zachary Scott Avatar asked Feb 10 '10 04:02

Zachary Scott


People also ask

How can we detect that an MVC controller is called by post or get?

You can check the Request. HttpMethod property.

What is Server MapPath in MVC?

For example, Server. MapPath() allows a path of "files/file1. doc". It uses the current context to determine the path of the current page, for example, and then creates the relative path from there.


1 Answers

Typically in ASP.NET, you would use a combination of Server.MapPath and File.Exists

Inside of a controller in ASP.NET MVC, you could use Request.MapPath as follows:

string filePath= Request.MapPath("~/Content/Images/image.jpg");
if( System.IO.File.Exists(filePath))
{
 //...
}
like image 85
markt Avatar answered Oct 20 '22 01:10

markt