找不到静态图像的控制器路径? asp.net mvc路由问题?

发布于 2024-08-26 03:15:57 字数 1881 浏览 7 评论 0原文

我有一个图像文件夹存储在 ~/Content/Images/

我通过最近加载这些图像

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

,图像未加载,并且我在错误日志中收到以下错误。奇怪的是,有些图像可以正常加载,而另一些则无法加载。

有人知道我的路线有什么问题吗?我是否缺少 /Content/ 文件夹的忽略路由?

我对 favicon.ico 和一堆其他图像文件也遇到同样的错误...

<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)

我当前的路线如下所示:

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
        );

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

安静被遗忘 2024-09-02 03:15:57

我会在第一个路由下方立即插入另一个被忽略的路由。

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

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

routes.IgnoreRoute("Content/Images/{*pathInfo}");
如果没有 2024-09-02 03:15:57

如果您查看解决方案资源管理器视图,我猜测您的内容文件夹位于项目的根目录中,以及控制器和视图的文件夹。尝试修改您的图像 src,如下所示......

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

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" />
偏爱你一生 2024-09-02 03:15:57

您需要声明到底部的不太具体的路线:

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 = "" }
);

但我不认为这是这里的问题。从异常情况来看,执行此应用程序的 Web 服务器似乎具有与 aspnet_isapi 过滤器的通配符映射,这意味着所有文件都将与 ASP.NET 运行时关联,甚至是静态文件。

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.

灯下孤影 2024-09-02 03:15:57
<img src="<%= Url.Content("~/Content/Images/Image.png")%>" alt="does this work?" />
<img src="<%= Url.Content("~/Content/Images/Image.png")%>" alt="does this work?" />
月光色 2024-09-02 03:15:57

你没有 routes.RouteExistingFiles = true; 某处吗?

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

从来不烧饼 2024-09-02 03:15:57

异常...无法找到或它没有实现IController不一定是错误的。实际上 /favicon.ico 不会解析为控制器,因此这意味着下一个模块(或者它是处理程序?)应该尝试处理该请求。所以在某种程度上这是一个“预期的例外”。

问题是当这个异常被记录并堵塞日志时。使用 log4net 时,将以下 元素添加到默认附加程序应该将其排除在外:

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文