3

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?

4

1 回答 1

1

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>");            
于 2013-10-02T00:45:09.783 回答