【发布时间】:2017-10-13 21:18:12
【问题描述】:
我们通过我们的 Asp .NET Web API 为网站提供文件:
public class Startup
{
public void Configuration(IAppBuilder app)
{
var clientHostname = System.Configuration.ConfigurationManager.AppSettings["ClientHostname"];
var staticFileOptions = new StaticFileOptions()
{
OnPrepareResponse = staticFileResponseContext =>
{
staticFileResponseContext.OwinContext.Response.Headers.Add("Cache-Control", new[] { "public", "max-age=0" });
}
};
app.MapWhen(ctx => ctx.Request.Headers.Get("Host").Equals(clientHostname), app2 =>
{
app2.Use((context, next) =>
{
if (context.Request.Path.HasValue == false || context.Request.Path.ToString() == "/") // Serve index.html by default at root
{
context.Request.Path = new PathString("/Client/index.html");
}
else // Serve file
{
context.Request.Path = new PathString($"/Client{context.Request.Path}");
}
return next();
});
app2.UseStaticFiles(staticFileOptions);
});
}
}
我想启用 HTTP 压缩。根据this MSDN documentation
在 IIS、Apache 或 Nginx 中使用基于服务器的响应压缩技术,其中中间件的性能可能与服务器模块的性能不匹配。无法使用时使用响应压缩中间件:
IIS 动态压缩模块
Apache mod_deflate 模块
NGINX 压缩解压
HTTP.sys 服务器(以前称为 WebListener)
红隼
所以我认为在我的实例中执行此操作的首选方法是使用 IIS 动态压缩模块。因此,我在我的 Web.config 中尝试了这个,作为测试,遵循this example:
<configuration>
<system.webServer>
<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
<dynamicTypes>
<add mimeType="*/*" enabled="true" />
</dynamicTypes>
<staticTypes>
<add mimeType="*/*" enabled="true" />
</staticTypes>
</httpCompression>
</system.webServer>
</configuration>
但是,响应标头不包含Content-Encoding,因此我认为它没有被压缩。我错过了什么?如何设置它以尽可能以最佳方式提供压缩服务?
我已验证我的客户端正在发送gzip, deflate, br 的Accept-Encoding 标头。
更新
我尝试在 IIS 中安装动态 HTTP 压缩,因为它没有默认安装。在我看来,我正在尝试静态地提供内容,但我认为这值得一试。我验证在 IIS 管理器中启用了静态和动态内容压缩。但是,我重新运行它,但仍然没有压缩。
更新 2
我意识到压缩在我们的 Azure 服务器上工作,但仍然不能在我的本地 IIS 上工作。
【问题讨论】:
-
不是所有类型的通配符,您是否在配置中尝试过特定的 mime 类型?
-
@Jasen 是的,我有,不幸的是无济于事。
标签: c# asp.net iis asp.net-web-api http-compression