1

I'm trying to display an image outside of the root folder of my asp .net mvc4 project - to no avail. The img tag I have tried inside the .master page and .ascx control is:

<img src='@Url.Action("someimage", "imagesController")' />

My controller looks like:

class imagesController : Controller
{
    public ActionResult someimage()
    {
        return File(@"C:\...\image.png", "image/png");
    }
}

The method above doesn't get called, instead I'm getting a: http://localhost:62372/Builder/@Url.Action(%22someimage%22,%20%22imagesController%22) Failed to load resource: the server responded with a status of 500 (Internal Server Error)

This is my routing, in case it matters:

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });

        //--default routing and default route--
        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Login", action = "OpenLogin", id = UrlParameter.Optional } // Parameter defaults
        );

    }

I have gathered more info about the error: Exception information:

Exception type: ArgumentException

Exception message: Illegal characters in path.

at System.IO.Path.CheckInvalidPathChars(String path)

at System.IO.Path.Combine(String path1, String path2)

...

11
  • 1
    You need to get the details for the general 500 error. Enable remote errors for IIS you haven't done so. But it reason is likely a file permission issue. Commented Aug 4, 2015 at 17:31
  • I'm using the VS2010 debugger, shall I switch to IIS for this? Commented Aug 4, 2015 at 17:31
  • Would it be possible to call the method in your controller and save the value in a ViewBag variable? You could then just call up that variable on the page and not worry about having a method to serve it up. Commented Aug 4, 2015 at 17:34
  • The method in the controller is not called in the first place Commented Aug 4, 2015 at 17:36
  • Right I am saying to not do it that way at all, you should just serve up that image location when you call up the ActionResult for that page. Commented Aug 4, 2015 at 17:39

2 Answers 2

2

One solution would be to look up the image path prior to page load and pass it to the view in a ViewBag variable. This does not fix your 500 error but you may not need to solve this issue in that way.

Controller:

public ActionResult ImagePage()
    {
       ViewBag.ImageSrc = GetMeMyImagePath();
       return View();
    }

View

<img src='@ViewBag.ImageSrc' />
Sign up to request clarification or add additional context in comments.

5 Comments

The path is looked up dynamically as part of user's input
You may want to look into using jquery and an Ajax request to handle updating that tag.
How is the user inputting the location of this image? If you have a string you could simply use some javascript to push it into the src attribute. Also if you do not know how something works Google it, Ajax is a very useful tool for getting data to your page.
The problem is that the src location is outside the project folder, and therefore some browsers block the image because of security. This is why I need to return the image from the controller
So then my suggestion would be to have a form that posts the image path back to your controller and process it there.
0

In the end I got it working by using this syntax instead:

<img src='http://domain/images/someimage' alt='' />

Comments

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.