与启用的ASP.NET Web API

问题描述:

我们服务的文件从我们的ASP .NET的Web API网站HTTP压缩:与启用的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,所以我不相信它被压缩。我错过了什么?我怎样才能以最好的方式将它设置为以压缩方式提供服务?

我确认我的客户正在发送一个Accept-Encoding标头gzip, deflate, br

更新

我试图在IIS安装动态HTTP压缩,因为它不是默认安装的。在我看来,我试图静态地提供内容,但我认为这值得一试。我验证了在IIS管理器中启用了静态和动态内容压缩。但是,我重新运行它,但仍然没有压缩。

更新2

我意识到压缩的工作我们Azure的服务器上,但仍无法与我的本地IIS。

+1

而不是所有类型的通配符,您是否尝试过在配置中使用特定的MIME类型? – Jasen

+1

@Jasen是的,我有,不幸的是无济于事。 –

我试着在一个空的4.7 .NET Web项目中启动,并且我得到了压缩,至少在index.html上。我安装了动态压缩,添加了几个Owin软件包,下面的web.config等工具。使用IIS/10

packages.config

<?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> 

的Web.config(为我工作不httpCompression)

<?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=\&quot;Web\&quot; /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> 

启动。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" }); 
       } 
      }; 
      ... 
      } 
    } 
} 

响应

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 
... 

我发现这三种资源是在任一ASP.Net WCF和Web API页面配置IIS动态压缩有用。我认为它也适用于.Net Core,但我还没有尝试过。前两个是有点老,但原则仍然适用:

https://blog.arvixe.com/how-to-enable-gzip-on-iis7/

https://www.hanselman.com/blog/EnablingDynamicCompressionGzipDeflateForWCFDataFeedsODataAndOtherCustomServicesInIIS7.aspx

https://docs.microsoft.com/en-us/iis/configuration/system.webserver/httpcompression/

具体做法是:

  • 是的,你确实需要动态HTTP压缩模块安装在IIS中,并启用
  • 确保动态压缩在IIS管理[你的服务器] /压缩下检查: Enable Dynamic Compression
  • 仔细检查的是,在客户端的请求报头中的MIME类型是在配置编辑器特意加入system.webServer/httpCompression/dynamicTypes/下,并且该类型处理” Enabled属性设置为True
  • 添加在web.config条目在上面的链接,对方的回答

最有可能这个缺什么你的Windows服务器上(我假设你在服务器上的工作概述Os)是Web的安装服务器IIS性能功能,其中有两个可以安装的子模块:Static Content ConpressionDynamic Content Compression

要检查自己是否安装运行的服务器管理器,选择Add Roles and Features,选择您的实例,在屏幕Server Roles扩大树节点Web Server (IIS)Web ServerPerformance并验证是否复选框表明Static Content ConpressionDynamic Content Compression安装,如果不检查并继续功能安装。然后在IIS管理器和网站设置中重复静态和动态压缩配置的所有设置步骤。它现在应该工作。

+0

我的操作系统是Windows 10 Home,所以我不确定在哪里可以找到服务器管理器? –

+0

确定它改变了一些问题,使我的上述答案无用。要检查您是否安装了IIS中所需的功能,请转到控制面板 - >程序和功能 - >打开或关闭Windows功能 - >查找Internet信息服务节点并展开它 - >展开万维网服务节点 - >展开性能功能节点 - 并验证是否选中了动态和静态内容压缩的复选框 - >如果不检查它们并继续安装。 –