5

以下代码有望成为使用 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 错误(或等效?)

4

1 回答 1

6

你会throw new HttpException(404, "Not found");的。显然,您的 web.config 部分中会有一个相应的页面customErrors向用户指示 404。

于 2011-02-01T19:15:22.400 回答