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?)?
You can check the Request. HttpMethod property.
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.
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))
{
//...
}
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