如何在GoDaddy共享windows托管上启用gzip压缩

时间:2015-01-16 09:27:21

标签: asp.net-mvc compression gzip

我已经完成了在GoDaddy共享windoes托管上启用gzpi压缩的研究,这是建议的解决方案:

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules runAllManagedModulesForAllRequests="true"/>

    <httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
      <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="*/*" enabled="false"/>
      </dynamicTypes>
      <staticTypes>
        <add mimeType="text/*" enabled="true"/>
        <add mimeType="message/*" enabled="true"/>
        <add mimeType="application/javascript" enabled="true"/>
        <add mimeType="*/*" enabled="false"/>
      </staticTypes>
    </httpCompression>
    <urlCompression doStaticCompression="true" doDynamicCompression="true"/>

  </system.webServer>

但目前这不起作用,我没有得到http响应头,Content-Encoding gzip。

有人可以帮我吗? 感谢

1 个答案:

答案 0 :(得分:0)

将其添加到Web.Config允许我的Godaddy网站压缩aspx页面响应。但是要压缩ASMX WebService响应,我必须另外将其添加到Global.asax:

void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
    var app = sender as HttpApplication;
    var acceptEncoding = app.Request.Headers["Accept-Encoding"];
    var prevUncompressedStream = app.Response.Filter;
    if (!string.IsNullOrEmpty(acceptEncoding))
    {
        acceptEncoding = acceptEncoding.ToLower();
        if (acceptEncoding.Contains("gzip"))
        {
            app.Response.Filter = new GZipStream(prevUncompressedStream, CompressionMode.Compress);
            app.Response.AppendHeader("Content-Encoding", "gzip");
        }
    }
    Response.Cache.VaryByHeaders["Accept-Encoding"] = true;
}