gzip 单个 asp.net 页面

发布于 2024-11-27 15:11:43 字数 142 浏览 0 评论 0 原文

是否可以对单个 asp.net 3.5 页面进行 gzip 压缩?我的网站托管在 IIS7 上,由于技术原因,我无法在整个网站范围内启用 gzip 压缩。 IIS7 是否可以选择 gzip 单个页面,或者我是否必须覆盖 OnPreRender 并编写一些代码来压缩输出?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

镜花水月 2024-12-04 15:11:43
        /// <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;
    }

我在一个名为 CompressionUtilities 的类中有这段代码。然后在您想要 GZIP 的页面中(或者在我的情况下,我想要 GZIP 的所有页面的共享基页)

    protected override void OnPreRender(EventArgs e)
    {

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

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

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

暗恋未遂 2024-12-04 15:11:43

您可以使用Blowery HttpCompress 模块。在 web.config 中,您可以指定要从压缩中排除的文件。

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文