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?

tereško
  • 58,060
  • 25
  • 98
  • 150

1 Answers1

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>");            
sharif y
  • 513
  • 4
  • 11
  • Thanks! I think this is a great solution! – watawata.coder Oct 03 '13 at 17:51
  • You don't have to use javascript inside the C# or html! Also you have always send EXCEPTIONS! not error codes or smthing. And wirte and exception handler. – Artem A Jan 22 '14 at 14:25
  • I don't see anything is wrong with sending js to view from code, why would you want user to see an exception, it should be logged in your backend, it's common practice to show error code anyway. – sharif y Jan 24 '14 at 04:57