Sco*_*y H 13 c# asp.net iis http-compression asp.net-web-api
我们从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);
});
}
}
Run Code Online (Sandbox Code Playgroud)
我想启用HTTP压缩.根据这个MSDN文档
在IIS,Apache或Nginx中使用基于服务器的响应压缩技术,其中中间件的性能可能与服务器模块的性能不匹配.当您无法使用时使用响应压缩中间件:
IIS动态压缩模块
Apache mod_deflate模块
NGINX压缩和解压缩
HTTP.sys服务器(以前称为WebListener)
红隼
所以我认为在我的实例中第一个更好的方法是使用IIS动态压缩模块.因此,我在我的Web.config中尝试了这个,作为测试,遵循以下示例:
<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>
Run Code Online (Sandbox Code Playgroud)
但是,响应标头不包含Content-Encoding
,所以我不相信它正在被压缩.我错过了什么?如何以最佳方式将其设置为压缩服务?
我已经验证我的客户端正在发送Accept-Encoding
标题gzip, deflate, br
.
更新
我尝试在IIS中安装Dynamic HTTP Compression,因为默认情况下没有安装它.在我看来,我试图静态地提供内容,但我认为这值得一试.我确认在IIS管理器中启用了静态和动态内容压缩.但是,我重新运行它但仍然没有压缩.
更新2
我意识到压缩工作在我们的Azure服务器上,但仍然不在我的本地IIS上.
我在一个空的 4.7 .NET Web 项目中尝试了您的启动,并且我得到了压缩,至少在 index.html 上是这样。我安装了动态压缩,添加了几个 Owin 包、下面的 web.config 等以使其正常工作。使用 IIS/10
包配置
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.5" targetFramework="net47" />
<package id="Microsoft.Net.Compilers" version="2.1.0" targetFramework="net47" developmentDependency="true" />
<package id="Microsoft.Owin" version="3.1.0" targetFramework="net47" />
<package id="Microsoft.Owin.FileSystems" version="3.1.0" targetFramework="net47" />
<package id="Microsoft.Owin.Host.SystemWeb" version="3.1.0" targetFramework="net47" />
<package id="Microsoft.Owin.StaticFiles" version="3.1.0" targetFramework="net47" />
<package id="Owin" version="1.0" targetFramework="net47" />
</packages>
Run Code Online (Sandbox Code Playgroud)
Web.config(在没有http压缩的情况下为我工作)
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
https://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<appSettings>
<add key="ClientHostname" value="localhost" />
<add key="owin:appStartup" value="WebApplication22.App_Start.Startup" />
</appSettings>
<system.web>
<compilation targetFramework="4.7" />
<httpRuntime targetFramework="4.7" />
</system.web>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" />
</compilers>
</system.codedom>
<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>
Run Code Online (Sandbox Code Playgroud)
Startup.cs(缩写)
using Microsoft.Owin;
using Microsoft.Owin.StaticFiles;
using Owin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication22.App_Start
{
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" });
}
};
...
}
}
}
Run Code Online (Sandbox Code Playgroud)
回复
HTTP/1.1 200 OK
Cache-Control: public,max-age=0
Content-Type: text/html
Content-Encoding: gzip
Last-Modified: Tue, 17 Oct 2017 22:03:20 GMT
ETag: "1d347b5453aa6fa"
Vary: Accept-Encoding
Server: Microsoft-IIS/10.0
X-Powered-By: ASP.NET
Date: Wed, 18 Oct 2017 02:27:34 GMT
Content-Length: 588
...
Run Code Online (Sandbox Code Playgroud)