2011-02-01 22 views
5

El código siguiente es de esperar una manera correcta para devolver una imagen que existe en el disco usando ASP.NET MVC 3:ASP.NET MVC FilePathResult: ¿Cómo devolver un archivo html no encontrado?

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 ??? 
} 

Si usted no encuentra el archivo, lo que podría devolver eso representaría un archivo HTML Error 404 (o equivalente?)

Respuesta

6

Usted throw new HttpException(404, "Not found");. Obviamente tendrías un corresponding page en la sección customErrors de tu web.config para indicar el 404 al usuario.

Cuestiones relacionadas