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?

1
  • This isn't a fileresult, the solution for an actionresult won't work for a actionresult Commented Oct 7, 2015 at 14:05

1 Answer 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>");            
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! I think this is a great solution!
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.