Playing with the httpCompression I relalized that IIS understand static files in MVC as dynamic content, so even if you tick the "Enable static content compression", but don't tick "Enable dynamic content compression", IIS will return the .css
and .js
files without compression:
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
But then if I tick the "Enable dynamic content compression" the files are compressed:
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
Even if I try to ignore the routes to ~/Content
and ~/Scripts
, these files are still understood as dynamic content:
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
);
}
I think this is probably because the web.config line that is needed for MVC but also forces all the request through the ASP.NET pipeline:
<modules runAllManagedModulesForAllRequests="true" />
UPDATE: I have tried to put this setting to false and happens the same.
Is there a way to avoid it? I don't want compression for my dynamic content but I do want it for my static content.
Or is the only way put the files somewhere else?
Cheers.
I think you'll find Rick has already answered your question here:
http://www.west-wind.com/weblog/posts/2011/May/05/Builtin-GZipDeflate-Compression-on-IIS-7x
I'm not sure why you are having that issue to be honest. Static Compression is working out of the box for me in MVC3, no special changes needed.
Like RickNZ said, make sure the mime types are accounted for properly in applicationhost.config
.
You can enable dynamic compression on a per-folder basis, from IIS Manager. Click on the folder name first in the Connections pane, then double-click on the Compression icon in the center pane, and select Enable dynamic compression.
Or, here's another, more brute force way:
Edit C:\Windows\System32\inetsrv\config\applicationHost.config (the IIS config file; make a copy first).
In the httpCompression section, remove the lines with mimeType="/" and mimeType="text/*", and replace them with mimeType="text/css" (an entry for JS is already there).
After re-starting IIS, dynamic compression should only be applied to your CSS & JS files, not your aspx output (which is text/html).
<modules runAllManagedModulesForAllRequests="true" />
is not required any more for IIS 7.5 SP1 or IIS7 SP1. It was required for MVC so requests to extensionless url go through the asp.net pipeline.
Extensionless url support is new in IIS7 SP1 and IIS7.5 SP1.
It is available for IIS7 as a patch that you have to request and install.
You will find it here with complete answers to your questions:
http://support.microsoft.com/kb/980368
In IIS config, check "mapping manager", "path" column. Maybe you have a mapping setup for these files.
Also check the * path with StaticFileHandler.
Did you remove any handler in your web.config ? Maybe by adding a statement ?
It should help (IIS7 MVC3):
Add another mapper to your 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>