开发者

controller path not found for static images? asp.net mvc routing issue?

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",                            开发者_如何学JAVA                  // Route name
            "{controller}/project/{projectid}/{action}/{searchid}",                           // URL with parameters
            new { controller = "Listen", action = "Index", searchid = "" }  // Parameter defaults
        );

Thanks!


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

routes.IgnoreRoute("Content/Images/{*pathInfo}");


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" />


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.


<img src="<%= Url.Content("~/Content/Images/Image.png")%>" alt="does this work?" />


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


The exception ... could not be found or it does not implement IController doesn't have to be in error. Actually /favicon.ico doesnt resolve to a controller so this means that the next module (or is it handler?) should try to handle the request. So in a way it's an "expected exception".

The problem is when this exception gets logged and clogs the logs. When using log4net, adding the following <filter> element to the default appender should keep it out:

<filter type="log4net.Filter.StringMatchFilter">
  <regexToMatch value="System.Web.HttpException \(0x80004005\): The controller for path '[^']*' was not found or does not implement IController\." />
  <acceptOnMatch value="false" />
</filter>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜