IIS 将 MVC 中的静态文件理解为动态内容

Posted

技术标签:

【中文标题】IIS 将 MVC 中的静态文件理解为动态内容【英文标题】:IIS understand static files in MVC as dynamic content 【发布时间】:2012-01-13 07:44:18 【问题描述】:

使用 httpCompression 我发现 ,因此即使您勾选“启用静态内容压缩”,但不要勾选“启用动态内容压缩”,IIS 将返回.css.js 文件而不压缩:

GET /MVCX/Content/Site.css HTTP/1.1
Host: localhost
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (Khtml, like Gecko) Chrome/15.0.874.121 Safari/535.2
Accept: text/css,*/*;
Referer: http://localhost/mvcx/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

HTTP/1.1 200 OK
Content-Type: text/css
Last-Modified: Mon, 05 Dec 2011 12:42:37 GMT
Accept-Ranges: bytes
ETag: "c79895e4bb3cc1:0"
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Mon, 05 Dec 2011 12:44:43 GMT
Content-Length: 1005

但是如果我勾选“启用动态内容压缩”,文件就会被压缩:

GET /MVCX/Content/Site.css HTTP/1.1
Host: localhost
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2
Accept: text/css,*/*;
Referer: http://localhost/mvcx/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

HTTP/1.1 200 OK
Content-Type: text/css
Content-Encoding: gzip
Last-Modified: Mon, 05 Dec 2011 12:42:37 GMT
Accept-Ranges: bytes
ETag: "c79895e4bb3cc1:0"
Vary: Accept-Encoding
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Mon, 05 Dec 2011 12:48:36 GMT
Content-Length: 522

即使我尝试忽略到~/Content~/Scripts 的路由,这些文件仍然被理解为动态内容:

    public static void RegisterRoutes(RouteCollection routes)
    
        routes.IgnoreRoute("resource.axd/*pathInfo");
        routes.IgnoreRoute("Content/*pathInfo");
        routes.IgnoreRoute("Scripts/*pathInfo");

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

我认为这可能是因为 MVC 所需的 web.config 行但也强制所有请求通过 ASP.NET 管道:

<modules runAllManagedModulesForAllRequests="true" />

更新:我尝试将此设置设置为 false,但结果相同。

有办法避免吗?我不想压缩我的动态内容,但我确实希望压缩我的静态内容。

或者是将文件放在其他地方的唯一方法?

干杯。

【问题讨论】:

我的回答(以及 Rick Strahl 的帖子)对您有帮助吗?好奇你是否曾经克服过这个肿块。 【参考方案1】:

您可以在 IIS 管理器中为每个文件夹启用动态压缩。首先在“连接”窗格中单击文件夹名称,然后双击中心窗格中的“压缩”图标,然后选择“启用动态压缩”。

或者,这是另一种更暴力的方式:

编辑 C:\Windows\System32\inetsrv\config\applicationHost.config(IIS 配置文件;先复制一份)。

在 httpCompression 部分,删除带有 mimeType="/" 和 mimeType="text/*" 的行,并用 mimeType="text/css" 替换它们(JS 的条目是已经在那里了)。

重新启动 IIS 后,动态压缩应该只应用于您的 CSS 和 JS 文件,而不是您的 aspx 输出(即 text/html)。

【讨论】:

我使用的是 MVC,所以没有文件夹。我尝试在主 Web.config 上进行设置,但没有成功。 默认情况下,我认为你不能在 web.config 中设置 urlCompression 标签;作为 IIS 设置,它位于 applicationHost.config 中。您应该能够编辑配置以设置正确的路径。您是否尝试过 applicationHost.config 中的 mimeType 更改?【参考方案2】:
<modules runAllManagedModulesForAllRequests="true" />

不再需要 IIS 7.5 SP1 或 IIS7 SP1。 MVC 需要它,因此对无扩展 url 的请求通过 asp.net 管道。

IIS7 SP1 和 IIS7.5 SP1 中新增了无扩展 url 支持。 它可用于 IIS7,作为您必须请求和安装的补丁程序。 您可以在这里找到问题的完整答案: http://support.microsoft.com/kb/980368

在 IIS 配置中,检查“映射管理器”、“路径”列。也许您有这些文件的映射设置。 还要用 StaticFileHandler 检查 * 路径。

您是否删除了 web.config 中的任何处理程序?也许通过添加一个语句?

【讨论】:

【参考方案3】:

我想你会发现 Rick 已经在这里回答了你的问题:

http://www.west-wind.com/weblog/posts/2011/May/05/Builtin-GZipDeflate-Compression-on-IIS-7x

说实话,我不确定您为什么会遇到这个问题。静态压缩在 MVC3 中对我来说是开箱即用的,不需要特殊更改。

就像 RickNZ 所说,确保在 applicationhost.config 中正确说明 mime 类型。

【讨论】:

【参考方案4】:

应该有帮助(IIS7 MVC3):

将另一个映射器添加到您的 web.config

<system.webServer>
    <modules runAllManagedModulesForAllRequests="false">
...
    </modules>

 <handlers>
      <remove name="UrlRoutingHandler" />     
      <clear />
      <add name="svc-ISAPI-4.0" path="*.svc" verb="*" modules="IsapiModule" scriptProcessor="%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" preCondition="classicMode,runtimeVersionv4.0,bitness32" />
      <add name="AssemblyResourceLoader-Integrated" path="WebResource.axd" verb="GET,DEBUG" type="System.Web.Handlers.AssemblyResourceLoader" modules="ManagedPipelineHandler" scriptProcessor="" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode" />
      <add name="svc-Integrated" path="*.svc" verb="*" type="System.ServiceModel.Activation.ServiceHttpHandlerFactory, System.ServiceModel.Activation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" modules="ManagedPipelineHandler" resourceType="Unspecified" requireAccess="Read" preCondition="integratedMode" />
     <add name="StaticFileHandler-html" path="*.html" verb="*" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" resourceType="Either" requireAccess="Read"/>
 ...     
      <add name="StaticFileHandler-css" path="*.css" verb="*" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" resourceType="Either" requireAccess="Read" />
      <add name="StaticFileHandler-js" path="*.js" verb="*" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" resourceType="Either" requireAccess="Read" />
      <add name="wildcard" path="*" verb="GET,HEAD,POST,DEBUG" type="System.Web.UI.PageHandlerFactory" modules="ManagedPipelineHandler" scriptProcessor="" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" preCondition="integratedMode" responseBufferLimit="4194304" />
      <add name="PageHandlerFactory-Folders" path="*" verb="*" type="System.Web.UI.PageHandlerFactory" modules="ManagedPipelineHandler" resourceType="Unspecified" requireAccess="Read" allowPathInfo="false" preCondition="integratedMode" />
      <add name="StaticFileHandler" path="*" verb="*" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" resourceType="Either" requireAccess="Read" />
    </handlers>

【讨论】:

以上是关于IIS 将 MVC 中的静态文件理解为动态内容的主要内容,如果未能解决你的问题,请参考以下文章

MVC3 里添加一个静态网页为啥访问不到?

将静态内容重写到 IIS 中的不同域

静态网页与动态网页的理解

asp.net mvc 共享内容目录

非常诡异的IIS下由配置文件加上svg的mime头导致整个网站的静态文件访问报错误

win2003怎么搭建路由