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)

...

Rafael
  • 1,099
  • 5
  • 23
  • 47
  • 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. – Jasen Aug 04 '15 at 17:31
  • I'm using the VS2010 debugger, shall I switch to IIS for this? – Rafael Aug 04 '15 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. – Zach M. Aug 04 '15 at 17:34
  • The method in the controller is not called in the first place – Rafael Aug 04 '15 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. – Zach M. Aug 04 '15 at 17:39
  • Since you are running the debugger you can step through that code. If the method is not called, the error may be cached so clear browser cache and try again. [Enable error details](http://stackoverflow.com/questions/5385714/deploying-website-500-internal-server-error) if you haven't already done so to see the reason for the 500 error. – Jasen Aug 04 '15 at 17:40
  • You are using mvc with razor sintax or classic sintax? – Richard Dias Aug 04 '15 at 17:49
  • It didn't make any difference. All I want to do is to display an image which is outside of the project folder. Chrome and FF block images with these paths – Rafael Aug 04 '15 at 18:00
  • One option you have is to store that image in temp storage and reference it there in your project, however that is way more complex. – Zach M. Aug 04 '15 at 18:04
  • See [this answer](http://stackoverflow.com/questions/28745052/asp-mvc-how-to-serve-image-file-video-out-of-my-root-folder) and just adjust your usage ``. – Jasen Aug 04 '15 at 18:24
  • How will then my img tag look like? – Rafael Aug 04 '15 at 18:28

2 Answers2

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' />
Zach M.
  • 1,188
  • 7
  • 22
  • 46
  • The path is looked up dynamically as part of user's input – Rafael Aug 04 '15 at 17:51
  • You may want to look into using jquery and an Ajax request to handle updating that tag. – Zach M. Aug 04 '15 at 17:57
  • 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. – Zach M. Aug 04 '15 at 18:03
  • 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 – Rafael Aug 04 '15 at 18:05
  • So then my suggestion would be to have a form that posts the image path back to your controller and process it there. – Zach M. Aug 04 '15 at 18:07
0

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

<img src='http://domain/images/someimage' alt='' />
Rafael
  • 1,099
  • 5
  • 23
  • 47