2011-02-01 8 views
5

Il seguente codice è, auspicabilmente, un modo corretto per restituire un'immagine che è presente sul disco utilizzando ASP.NET MVC 3:ASP.NET MVC FilePathResult: come restituire un file html non trovato?

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

Se non si trova il file, quello che si potrebbe tornare che rappresenterebbe un HTML Errore 404 (o equivalente?)

risposta

6

Si throw new HttpException(404, "Not found");. Ovviamente si dovrebbe avere un corresponding page nella sezione customErrors del proprio web.config per indicare il 404 all'utente.