2011-02-01 47 views
5

下面的代碼是希望以正確的方式返回磁盤上存在的圖像使用ASP.NET MVC 3:ASP.NET MVC FilePathResult:如何返回未找到的html文件?

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錯誤(或等效?)

回答

6

你會throw new HttpException(404, "Not found");。很明顯,你的web.config的customErrors部分有一個corresponding page來向用戶指示404。

相關問題