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

The following code, we hope, is the 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 didn’t find the file so that you could return, it will represent HTML 404 error (or equivalent?)

+5
source share
1 answer

You would throw new HttpException(404, "Not found");. Obviously, you will have the corresponding page in the section of customErrorsyour web.config to tell the 404 user.

+6
source

All Articles