zoukankan      html  css  js  c++  java
  • 编程经验:高性能.NET WEB开发(1)http压缩

    关于Aspx页面优化的几种方法

     

    Http压缩

    1、在项目里面新建一个CompressionModule.cs类,命名空间CanYou

     

    #region Using

    using System;

    using System.Net;

    using System.Web;

    using System.Web.Caching;

    using System.IO;

    using System.IO.Compression;

    #endregion

     

    namespace CanYou

    {

        ///<summary>

        /// Compresses the output using standard gzip/deflate.

        ///</summary>

        public sealed class CompressionModule : IHttpModule

        {

            #region IHttpModule Members

            ///<summary>

            /// Disposes of the resources (other than memory) used by the module

            /// that implements <see cref="T:System.Web.IHttpModule"></see>.

            ///</summary>

     

            void IHttpModule.Dispose()

            {

                // Nothing to dispose;

            }

     

            ///<summary>

            /// Initializes a module and prepares it to handle requests.

            ///</summary>

            ///<param name="context">An <see cref="T:System.Web.HttpApplication"></see>

            /// that provides access to the methods, properties, and events common to

            /// all application objects within an ASP.NET application.

            ///</param>

     

            void IHttpModule.Init(HttpApplication context)

          {

           context.PreRequestHandlerExecute += new EventHandler(context_PostReleaseRequestState);

     

          }

     

            #endregion

    private const string GZIP = "gzip";

    private const string DEFLATE = "deflate";

    #region Compress page

     

            ///<summary>

            /// Handles the BeginRequest event of the context control.

            ///</summary>

            ///<param name="sender">The source of the event.</param>

            ///<param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>

     

            void context_PostReleaseRequestState(object sender, EventArgs e)

            {

                HttpApplication app = (HttpApplication)sender;

                if (app.Context.CurrentHandler is System.Web.UI.Page && app.Request["HTTP_X_MICROSOFTAJAX"] == null)

                {

                    if (IsEncodingAccepted(DEFLATE))

                    {

                        app.Response.Filter = new DeflateStream(app.Response.Filter, CompressionMode.Compress);

                        SetEncoding(DEFLATE);

                    }

     

                    else if (IsEncodingAccepted(GZIP))

                    {

                        app.Response.Filter = new GZipStream(app.Response.Filter, CompressionMode.Compress);

                        SetEncoding(GZIP);

                    }

                }

            }

     

            ///<summary>

            /// Checks the request headers to see if the specified

            /// encoding is accepted by the client.

            ///</summary>

     

            private static bool IsEncodingAccepted(string encoding)

            {

                HttpContext context = HttpContext.Current;

                return context.Request.Headers["Accept-encoding"] != null && context.Request.Headers["Accept-encoding"].Contains(encoding);

            }

     

            ///<summary>

            /// Adds the specified encoding to the response headers.

            ///</summary>

            ///<param name="encoding"></param>

     

            private static void SetEncoding(string encoding)

            {

                HttpContext.Current.Response.AppendHeader("Content-encoding", encoding);

            }

     

            #endregion

     

        }

    }

     

    2、按如下增加及修改项目的web.config

    <system.web>

    <httpModules>

    <add name="CompressionModule" type="CanYou.CompressionModule"/>

        </httpModules>

     </system.web>

     

    相关文章:

    Ⅰ、页面压缩的使用

     Ⅱ、EnableViewState

     Ⅲ、控件缓存

  • 相关阅读:
    The Best Seat in ACM Contest
    确定比赛名次
    Red and Black
    Can you find it?
    胜利大逃亡
    Reward
    DXUT编译指南(转)
    逐顶点和逐像素光照
    转战DX
    hlsl之ambient
  • 原文地址:https://www.cnblogs.com/Gemgin/p/3136318.html
Copyright © 2011-2022 走看看