1

我使用返回錯誤信息/爲FileResult頁在ASP.NET MVC

<img src="@Url.Action("getImage", "AccessFile", new { imagePath= @ViewData["imagePath"] })"/> 

在視圖中顯示存在作爲服務器的本地文件的圖像。在AccessFileController
的getImage動作看起來就像

public ActionResult getImage(string imagePath) 
{ 
    if (!System.IO.File.Exists(imagePath)) 
    { 
     //Log 
     return ????????? 
    } 
    else 
    { 
     return base.File(imagePath, "image/jpg"); 
    } 

}

我的問題是,我應該把在?????????允許用戶看到某種錯誤信息?
我試圖

  • 回報新HttpNotFoundResult();
  • return RedirectToAction(「ExpectedError」,「Error」);

但它只返回空圖像與它的一個破碎的圖標。
任何好的解決方案?

+0

這不是一個fileresult,該解決方案行動結果不會爲行動結果 –

回答

0

您可以顯示預定義的圖像空請求,

return Content(imagePath2); 

,或者您可以顯示一個JavaScript,然後就可以顯示一個錯誤

return Content("<script language='javascript' type='text/javascript'>alert('Image not found!');</script>");    
+0

謝謝!我認爲這是一個很好的解決方案! –

+0

你不必在C#或HTML裏面使用javascript!你也一直髮送EXCEPTIONS!不是錯誤代碼或者什麼。和wirte和異常處理程序。 –

+0

我沒有看到發送js從代碼中查看的任何錯誤,爲什麼你希望用戶看到一個異常,它應該記錄在你的後端,通常的做法是顯示錯誤代碼。 –

相關問題