Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an image folder stored at ~/Content/Images/

I am loading these images via

<img src="/Content/Images/Image.png" />

Recently, the images aren't loading and I am getting the following errors in my error log. What's weird is that some images load fine, while others do not load.

Anyone have any idea what is wrong with my routes? Am I missing an ignore route for the /Content/ folder?

I am also getting the same error for favicon.ico and a bunch of other image files...

<Fatal> -- 3/25/2010 2:32:38 AM -- System.Web.HttpException: The controller for path '/Content/Images/box_bottom.png' could not be found or it does not implement IController.
at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(Type controllerType)
at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName)
at System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext)
at System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext)
at System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

My current routes look like this:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );

        routes.MapRoute(
            "ControllerDefault",                                              // Route name
            "{controller}/project/{projectid}/{action}/{searchid}",                           // URL with parameters
            new { controller = "Listen", action = "Index", searchid = "" }  // Parameter defaults
        );

Thanks!

share|improve this question

5 Answers 5

<img src="<%= Url.Content("~/Content/Images/Image.png")%>" alt="does this work?" />
share|improve this answer
1  
oops, answered with a question. -1 –  ScottG Apr 16 '10 at 2:31

You need to declare less specific routes to the bottom:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
    "ControllerDefault",
    "{controller}/project/{projectid}/{action}/{searchid}",
    new { controller = "Listen", action = "Index", searchid = "" }
);

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = "" }
);

But I don't think that this is the problem here. From the exception it seems that the web server executing this application has a wildcard mapping with the aspnet_isapi filter meaning that all file will be associated with the ASP.NET runtime even the static files.

share|improve this answer
    
Yep, you can do that little trick in IIS where you first fake the Content folder as being a virtual directory, remove the wild card mapping, then remove the Content folder as being a virtual directory. This should solve the problem. –  David Mar 25 '10 at 9:02

If you look at your solution explorer view, I'm guessing that your Content folder is in the root of the project along with a folder for Controllers and Views. Trying modifying your image src as shown below...

<img src="../../Content/Images/Image.png" />
share|improve this answer
1  
That wouldn't work for me... I need to use the root path (/Content/Images) because those images are referenced in css files that stand alone in dev, and are minified and combined in production (thus losing relative location). But why would this help anyway? –  rksprst Mar 25 '10 at 6:22
    
@rksprst: I've had the same issue and solved it like this. Right now /Content/Images/Image.png will look within your views action folder for the image... It is not pulling from the ~/Content/Images/ folder. –  RSolberg Mar 25 '10 at 15:18

You don't have routes.RouteExistingFiles = true; somewhere do you?

share|improve this answer
    
No, I don't have that anywhere. –  rksprst Mar 25 '10 at 4:35

I would insert another ignored route immediately under the first one.

routes.IgnoreRoute("Content/Images/{*pathInfo}");
share|improve this answer
    
I'm going to try this... but the prob only occurs on staging (not dev) so won't be until tomm that I can test. –  rksprst Mar 25 '10 at 6:44
    
Did you try that out? –  Sayed Ibrahim Hashimi Mar 26 '10 at 14:51
    
Yes, still the same error. –  rksprst Mar 28 '10 at 1:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.