考虑一个用于定位和提供文件的 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)?
发布评论
评论(2)
如果其他人遇到此问题,解决方案是您需要让 MVC 知道不要处理实际静态文件所在的文件夹中的请求:
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:
除了 Daniel Crenna 的回答之外,您还需要在 system.webServer 部分的 web.confug 文件中添加:
In adiition to Daniel Crenna's answer, you need to add in web.confug file in system.webServer section: