【问题标题】:asp.net mvc bundles gzip compresstion not workasp.net mvc 捆绑 gzip 压缩不起作用
【发布时间】:2017-07-08 01:13:51
【问题描述】:

我的捆绑配置是这样的:

public class BaseBundleConfig
{
    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new ScriptBundle("~/bundles/Models").IncludeDirectory("~/Scripts/models/", "*.js", true));

        bundles.Add(new ScriptBundle("~/bundles/framework").Include(
                                            "~/Scripts/framework/frameworkmodels.js",
                                            "~/Scripts/framework/appbase.js",
                                            "~/Scripts/framework/directives/directives.js",
                                            "~/Scripts/framework/services/Services.js",
                                            "~/Scripts/framework/controllers/controllers.js",
                                            "~/Scripts/framework/filter/filters.js",
                                            "~/Scripts/app/filter/customFilters.js"
                                            ).Include("~/Scripts/app.js"));


        bundles.Add(new StyleBundle("~/Content/css").Include(
                                        "~/Content/libs/bootstrap/bootstrap.min.css",
                                        "~/Content/libs/bootstrap/bootstrap-theme.min.css",                                     
                                        "~/Content/libs/bootstrap/angular-csp.css",
                                        "~/Content/libs/bootstrap/mainStyle.css"
                                        ));

        BundleTable.EnableOptimizations = true;

    }
}

我在 index.cshtml 中使用了 bundle,如下所示:

......
@System.Web.Optimization.Scripts.Render("~/bundles/framework")

.....
@System.Web.Optimization.Styles.Render("~/Content/css")

在 IIS 8.5 上发布后。捆绑和缩小按预期工作,但对于 ScriptBundle 和 StyleBundle,捆绑未压缩。响应内容类型始终是 text/javascript 或 text/css。 为什么 gzip 压缩不起作用?

动态和静态内容压缩都在 web.config 中设置为 true

<system.webServer>

    <urlCompression doStaticCompression="true" doDynamicCompression="true" />
</system.webServer>

MVC 程序集的版本: System.Web.Mvc 5.2.3.0 System.Web.Optimization 1.1.0 并且动态压缩模块也安装在服务器上。

新信息: 两张截图,首先是IIS请求跟踪日志 显示动态压缩模块正在执行它的工作,第二个是该请求的 chrome 网络选项卡响应标头。为什么 Transfer-Encoding:chunked 和 response 不是 gzip :\

感谢您的想法

【问题讨论】:

标签: c# asp.net-mvc asp.net-mvc-5 bundling-and-minification iis-8.5


【解决方案1】:

&lt;urlCompression&gt; 之前或之后将&lt;httpCompression&gt; 部分添加到您的web.config。这是我的:

<httpCompression directory="%SystemDrive%\websites\_compressed" minFileSizeForComp="1024">
    <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
    <dynamicTypes>               
        <add mimeType="text/*" enabled="true" />
        <add mimeType="message/*" enabled="true" />
        <add mimeType="application/javascript" enabled="true" />
        <add mimeType="application/x-javascript" enabled="true" />
        <add mimeType="application/json" enabled="true" />
        <add mimeType="*/*" enabled="false" />
    </dynamicTypes>
    <staticTypes>
        <add mimeType="text/*" enabled="true" />
        <add mimeType="message/*" enabled="true" />
        <add mimeType="application/javascript" enabled="true" />
        <add mimeType="application/x-javascript" enabled="true" />
        <add mimeType="application/json" enabled="true" />
        <add mimeType="*/*" enabled="false" />
    </staticTypes>
</httpCompression>

请注意,除非捆绑包为 1024 字节或更大 (minFileSizeForComp),否则这不会启动,因此您不会浪费时间压缩已经非常小的文件。

另请注意,您需要确保 gzip.dll 在此文件夹中。

【讨论】:

  • 刚刚添加配置但没有成功
  • 抱歉没用。您是否已经完成了詹姆斯提供的链接中的所有故障排除?有很多因素可能会导致 gzip 无法正常工作,正如您所期望的那样。 stackoverflow.com/questions/6938713/…
【解决方案2】:

我找到了问题点,客户端和 IIS 之间的硬件防火墙解压缩 gzip 数据并在处理内容后,将其分块发送到客户端,当我从公司外部的其他客户端尝试时,gzip 压缩按预期工作。

【讨论】:

  • 经过 3 个小时的摆弄服务器后,问题解决了。无论如何,这是可以克服的吗?
猜你喜欢
  • 2015-10-10
  • 2018-03-27
  • 2021-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-20
  • 2018-04-19
相关资源
最近更新 更多