asp.net-mvc - IIS将MVC中的静态文件理解为动态内容

标签 asp.net-mvc asp.net-mvc-3 iis-7 iis-7.5 http-compression

玩 httpCompression 我意识到 IIS 将 MVC 中的静态文件理解为动态内容,因此即使您勾选“ 启用静态内容压缩 ”,但不要勾选“ 启用动态内容压缩 ”,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 已经在这里回答了你的问题:

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

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

就像 RickNZ 所说的那样,确保在 applicationhost.config 中正确考虑了 mime 类型。 .

关于asp.net-mvc - IIS将MVC中的静态文件理解为动态内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8387500/

相关文章:

c# - 当 # 提供而不是时,如何在 C# 中分离查询字符串?

asp.net - MVC 4 PartialViewResult 缓存 jQuery $.post

c# - Ajax.ActionLink 不调用 Controller Action

iis - 更新配置文件中的 IIS 绑定(bind),而不是 GUI

asp.net - 发送 HTTP header 后,服务器无法附加 header ?

asp.net-mvc - MVC 4.0 使用 HttpResponse.RemoveOutputCacheItem 清除输出缓存

javascript - 电子邮件验证未捕获必填字段

javascript - 如果我使用 AJAX 调用它,如何取消 MVC 操作?

asp.net - 如何使用 MVC3 显示数据库中的多个图像

c# - 数字已被星号 Asp.net webform 掩盖