ASP.NET MVC 在 IIS7 中不使用控制器进行显式文件路由

发布于 2024-08-26 20:52:33 字数 922 浏览 8 评论 0 原文

考虑一个用于定位和提供文件的 StaticResourceController。

我已经为“favicon.ico”设置了一条显式路由,它将使用 StaticResourceController 处理对此文件的请求:

routes.MapRoute(
        "favicon",
        "favicon.ico",
        new { controller = "StaticResource", action = "Get", file = "favicon.ico", area="root"},
        new[] { "Dimebrain.Mvc.Controllers" }
        );

在 IIS6 中,当请求 http://localhost:8080/favicon.ico

不幸的是,当我部署到 IIS7 http://localhost/favicon.ico 返回 IIS 生成的 404 时,大概是因为它实际上是在 Web 根文件夹中查找 favicon.ico,但该文件夹并不存在。

我在 StaticResourceController 中发生的事情已经够多了,这对我的应用程序来说不是一件好事,特别是因为它是多租户的并且 favicon.ico 文件可以更改。我已将 Web 服务器模块设置为处理每个请求并覆盖 RouteCollection 以忽略 RouteExistingFiles 的文件检查。

为什么 UrlRoutingModule 在 IIS7 中妨碍我并强制从磁盘提供静态文件 (404)?

Consider a StaticResourceController that locates and serves files.

I've set up an explicit route for "favicon.ico" that will handle the request for this file using StaticResourceController:

routes.MapRoute(
        "favicon",
        "favicon.ico",
        new { controller = "StaticResource", action = "Get", file = "favicon.ico", area="root"},
        new[] { "Dimebrain.Mvc.Controllers" }
        );

In IIS6 the expected result occurs when making a request for http://localhost:8080/favicon.ico.

Unfortunately when I deploy to IIS7 http://localhost/favicon.ico returns an IIS-generated 404, presumably because it's actually looking for the favicon.ico in the web root folder, where it doesn't exist.

I have enough happening in StaticResourceController that this isn't a good thing for my application, especially since it is multi-tenant and the favicon.ico file can change. I've set my web server modules to handle every request and override the RouteCollection to disregard file checks with RouteExistingFiles.

Why is the UrlRoutingModule getting in my way in IIS7 and forcing serving the static file from disk (404)?

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

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

发布评论

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

评论(2

巷雨优美回忆 2024-09-02 20:52:33

如果其他人遇到此问题,解决方案是您需要让 MVC 知道不要处理实际静态文件所在的文件夹中的请求:

// Make sure MVC is handling every request for static files
routes.RouteExistingFiles = true;

// Don't process routes where actual static resources live
routes.IgnoreRoute("content/{*pathInfo}");
routes.IgnoreRoute("scripts/{*pathInfo}");
routes.IgnoreRoute("areas/admin/content/{*pathInfo}");
routes.IgnoreRoute("areas/admin/scripts/{*pathInfo}");

In case anyone else runs into this problem, the solution is you need you to let MVC know not to process requests in folders where your actual static files live:

// Make sure MVC is handling every request for static files
routes.RouteExistingFiles = true;

// Don't process routes where actual static resources live
routes.IgnoreRoute("content/{*pathInfo}");
routes.IgnoreRoute("scripts/{*pathInfo}");
routes.IgnoreRoute("areas/admin/content/{*pathInfo}");
routes.IgnoreRoute("areas/admin/scripts/{*pathInfo}");
谁的新欢旧爱 2024-09-02 20:52:33

除了 Daniel Crenna 的回答之外,您还需要在 system.webServer 部分的 web.confug 文件中添加:

<modules runAllManagedModulesForAllRequests="true"/>

In adiition to Daniel Crenna's answer, you need to add in web.confug file in system.webServer section:

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