Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在MVC2中,如何使用C#确定服务器上存在文件?_C#_Asp.net Mvc_.net - Fatal编程技术网

在MVC2中,如何使用C#确定服务器上存在文件?

在MVC2中,如何使用C#确定服务器上存在文件?,c#,asp.net-mvc,.net,C#,Asp.net Mvc,.net,我知道你可以做到: if( System.IO.File.Exists( @"C:\INetPub\MVCWebsite\Content\Images\image.jpg") ) { ... } 您可以对MVC中的引用文件执行以下操作: Url.Content("~/Content/Images/image.jpg") 那么有没有一种方法可以相对地检查“~/Content/Images/image.jpg”是否存在(在MVC中?通常在ASP.NET中,您会使用Server.MapPa

我知道你可以做到:

if( System.IO.File.Exists(
    @"C:\INetPub\MVCWebsite\Content\Images\image.jpg") ) { ... }
您可以对MVC中的引用文件执行以下操作:

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

那么有没有一种方法可以相对地检查“~/Content/Images/image.jpg”是否存在(在MVC中?

通常在ASP.NET中,您会使用Server.MapPath和File.exists的组合

在ASP.NET MVC中的控制器内部,可以使用Request.MapPath,如下所示:

string filePath= Request.MapPath("~/Content/Images/image.jpg");
if( System.IO.File.Exists(filePath))
{
 //...
}

不确定是否应该改用
HostingEnvironment.MapPath
?最好使用HostingEnvironment.VirtualPathProvider.FileExists(“~/path/to/file”)检查文件是否存在