开发者

gzip a single asp.net page

开发者 https://www.devze.com 2023-03-25 23:52 出处:网络
Is it possible to gzip a single asp.net 3.5 page? my site is hosted on IIS7 and for technical reasons I cannot enable gzip compression site wide. does IIS7 have an option to gzip individual pages or w

Is it possible to gzip a single asp.net 3.5 page? my site is hosted on IIS7 and for technical reasons I cannot enable gzip compression site wide. does IIS7 have an option to gzip individual pages or will I have to override OnPreRender and write some code to compress the outpu开发者_StackOverflow社区t?


        /// <summary>
    /// Sets up the current page or handler to use GZip through a Response.Filter
    /// IMPORTANT:  
    /// You have to call this method before any output is generated!
    /// </summary>
    public static void GZipEncodePage()
    {
        HttpResponse response = HttpContext.Current.Response;

        if (IsGZipSupported())
        {
            string acceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"];
            if (acceptEncoding.Contains("deflate"))
            {
                response.Filter = new System.IO.Compression.DeflateStream(response.Filter,
                                                                          System.IO.Compression.CompressionMode.
                                                                              Compress);
                response.AppendHeader("Content-Encoding", "deflate");
            }
            else
            {
                response.Filter = new System.IO.Compression.GZipStream(response.Filter,
                                                                       System.IO.Compression.CompressionMode.
                                                                           Compress);
                response.AppendHeader("Content-Encoding", "gzip");
            }
        }

        // Allow proxy servers to cache encoded and unencoded versions separately
        response.AppendHeader("Vary", "Content-Encoding");
    }

    /// <summary>
    /// Determines if GZip is supported
    /// </summary>
    /// <returns></returns>
    public static bool IsGZipSupported()
    {
        string acceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"];

        if (!string.IsNullOrEmpty(acceptEncoding) &&
            (acceptEncoding.Contains("gzip") || acceptEncoding.Contains("deflate")))
        {
            return true;
        }
        return false;
    }

I have this code in a class called CompressionUtilities. Then in the page you want to GZIP (or in my case, a shared base page for all the pages I want to GZIP)

    protected override void OnPreRender(EventArgs e)
    {

        base.OnPreRender(e);
        CompressionUtilities.GZipEncodePage();
    }

Sourced here: http://www.west-wind.com/weblog/posts/2007/Feb/05/More-on-GZip-compression-with-ASPNET-Content


You can use Blowery HttpCompress module. In the web.config you can specificy the files you want to exclude from compression.

   <httpCompress preferredAlgorithm="gzip" compressionLevel="high">
      <excludedMimeTypes>
        <add type="application/pdf"/>
      </excludedMimeTypes>
      <excludedPaths>
        <add path="/pathToExclude"/>
        <add path="WebResource.axd"/>
        <add path="ScriptResource.axd"/>
      </excludedPaths>
    </httpCompress>
0

精彩评论

暂无评论...
验证码 换一张
取 消