2011-12-05 59 views
6

使用httpCompression播放IIS将MVC中的静态文件理解为动态内容,因此即使勾选了“启用静态内容压缩”,但却不勾选“ 启用动态内容压缩”,IIS将返回.css.js文件,而无需压缩:IIS将MVC中的静态文件理解为动态内容

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

我想这大概是因为这是需要在web.config线MVC也迫使通过ASP.NET管道中的所有要求:

<modules runAllManagedModulesForAllRequests="true" /> 

更新:我试图把这个设置为false,并发生一样。

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

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

干杯。

+0

我的回答(和Rick施特拉尔的职位)帮你呢?好奇,如果你有过这个团。 –

回答

0

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

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

编辑C:\ WINDOWS \ SYSTEM32 \ INETSRV \设置\的applicationHost.config(IIS的配置文件;先构建一个备份)。

在httpCompression部,取出线,mime类型=“/‘和mime类型=’文本/ *”,并与mime类型=‘文本/ CSS’(对于JS的条目已经存在)替换它们。

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

+0

我正在使用MVC,所以没有文件夹。我试图在主Web.config上设置它,但它不起作用。 – vtortola

+0

默认情况下,我不认为你可以在web.config中设置urlCompression标签;作为IIS设置,它位于applicationHost.config中。您应该能够编辑配置以设置正确的路径。你有没有尝试在applicationHost.config中更改mimeType? – RickNZ

0
<modules runAllManagedModulesForAllRequests="true" /> 

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

扩展网址支持是IIS7 SP1和IIS7.5 SP1中的新增功能。 它可用于IIS7作为您必须请求和安装的修补程序。 您将在这里找到它完全解答您的疑问: http://support.microsoft.com/kb/980368

在IIS配置,勾选“映射管理”,“路径”栏。也许你有这些文件的映射设置。 也用StaticFileHandler检查*路径。

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

0

它应该帮助(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>