質問

I'm using

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

in the View to display image that exists as a local file in the server.
getImage action in AccessFileController will look like

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

}

My question is, what should I put in ????????? to allow user to see some kind of error message?
I tried

  • return new HttpNotFoundResult();
  • return RedirectToAction("ExpectedError", "Error");

but it only returns empty image with a broken icon on it.
Any good solution?

役に立ちましたか?

解決

You can show a predefined image for empty request,

return Content(imagePath2);

or you can show a javascript which then you can show an error

    return Content("<script language='javascript' type='text/javascript'>alert('Image not found!');</script>");            
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top