开发者

ASP.NET MVC FilePathResult: How to return an html file not found?

开发者 https://www.devze.com 2023-02-08 11:54 出处:网络
The following code is hopefully a correct way to return an image that exists on disk using ASP.NET MVC 3:

The following code is hopefully a correct way to return an image that exists on disk using 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 ???   
}

If you don't find the file, what could you return that would represent an HTML 404 error (or equivalent?)


You would throw new HttpException(404, "Not found");. Obviously you would have a corresponding page in the customErrors section of your web.config to indicate the 404 to the user.

0

精彩评论

暂无评论...
验证码 换一张
取 消