asp.net mvc 捆绑 gzip 压缩无效
asp.net mvc bundles gzip compresstion not work
我的 Bundle Config 是这样的:
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 的捆绑 未压缩 。响应 content-type 始终是 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 网络选项卡响应 headers。为什么 Transfer-Encoding:chunked 而响应不是 gzip :\
谢谢你的想法
在 <urlCompression>
之前或之后将 <httpCompression>
部分添加到您的 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 在此文件夹中。
我发现了问题点,客户端和 IIS 之间的硬件防火墙解压缩 gzip 数据并在处理内容后,将其分块发送给客户端,当我从公司外部的其他客户端尝试时,gzip 压缩工作如我所料。
我的 Bundle Config 是这样的:
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 的捆绑 未压缩 。响应 content-type 始终是 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 网络选项卡响应 headers。为什么 Transfer-Encoding:chunked 而响应不是 gzip :\
谢谢你的想法
在 <urlCompression>
之前或之后将 <httpCompression>
部分添加到您的 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 在此文件夹中。
我发现了问题点,客户端和 IIS 之间的硬件防火墙解压缩 gzip 数据并在处理内容后,将其分块发送给客户端,当我从公司外部的其他客户端尝试时,gzip 压缩工作如我所料。