以下代码有望成为使用 ASP.NET MVC 3 返回磁盘上存在的图像的正确方法:
public FilePathResult GetThumbnail(string imageName)
{
if( !String.IsNullOrEmpty(imageName) &&
Regex.IsMatch(imageName, @"^p\d{10}.jpg$"))) ) // p0000000000.jpg
{
var homePath = Server.MapPath("~/Content/previews");
var imagePath = Path.Combine( homePath, imageName );
if( System.IO.File.Exists(imagePath) )
return this.File(imagePath, "image/jpeg");
}
return ???
}
如果您没有找到该文件,您可以返回什么表示 HTML 404 错误(或等效?)