1. 程式人生 > >asp.net 使用HttpModule對全站輸出的動態頁面的HTML內容進行修改,不會錯亂

asp.net 使用HttpModule對全站輸出的動態頁面的HTML內容進行修改,不會錯亂

http://blog.csdn.net/lrxin/article/details/40861039

配置方法:

     <httpModules>
      <add name="FileEditModule" type="Framework.FileEditModule, Framework" />

    </httpModules>


<pre name="code" class="csharp">public class FileEditModule : System.Web.IHttpModule
    {
        public void Dispose() { }

        public void Init(HttpApplication application)
        {
            application.EndRequest += new EventHandler(application_EndRequest);
        }

        void application_EndRequest(object sender, EventArgs e)
        {
            try
            {
                HttpApplication application = (HttpApplication)sender;

                Uri url = application.Request.Url;
                // 如果請求Url需要驗證
                string[] ss = application.Request.FilePath.Split('.');
                string extension = ss[ss.Length - 1];
                extension = extension.ToLower();
                string[] extensions = new string[] { 
                "ashx",
                "aspx",
                "asmx"
                };
                if (extensions.Contains(extension))
                {
                    string html = GetRenderHtml(null, application.Response);
                    //....對html進行操作
                    application.Response.ClearContent();
                    application.Response.Write(html);

                }
            }
            catch (Exception ex) { }
        }
public string GetRenderHtml(HtmlTextWriter writer, HttpResponse Response)
        {
            //var Response = this.Page.Response;

            //Response.Output其實就是一個HttpWriter,Response.OutputStream其實就是HttpResponseStream,Object.ReferenceEquals (Response.Output,(Response.OutputStream as HttpResponseStream)._writer)為true

            BindingFlags bind = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.GetField;
            //因為HttpWriter._charBuffer這個字元陣列的長度是1024,
            //所以推測,一旦某一段字串超過1024就會建立一個IHttpResponseElement,
            //然後加入到HttpWriter._buffers,HttpWriter._buffers的每一個元素都實現了IHttpResponseElement介面,
            //具體型別可能是HttpResponseUnmanagedBufferElement,HttpSubstBlockResponseElement等型別
            ArrayList arr = (ArrayList)Response.Output.GetType().GetField("_buffers", bind).GetValue(Response.Output);

            Assembly systemWeb = Assembly.Load("System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
            Type type = systemWeb.GetType("System.Web.IHttpResponseElement");
            MethodInfo method = type.GetMethod("GetBytes");
            StringBuilder sb = new StringBuilder(5000);
            //遍歷每一個buffer,獲取buffer裡儲存的字元陣列,然後轉換為字串
            for (int i = 0; i < arr.Count; i++)
            {
                byte[] buffer = (byte[])method.Invoke(arr[i], null);
                //使用當前編碼得出已經儲存到HttpWriter._buffers中的字串
                sb.Append(Response.ContentEncoding.GetString(buffer));
            }
            //獲取HttpWriter的字元陣列緩衝區
            char[] charBuffer = (char[])Response.Output.GetType().GetField("_charBuffer", bind).GetValue(Response.Output);
            int charBufferLength = (int)Response.Output.GetType().GetField("_charBufferLength", bind).GetValue(Response.Output);
            int charBufferFree = (int)Response.Output.GetType().GetField("_charBufferFree", bind).GetValue(Response.Output);
            //charBufferLength - charBufferFree 等於字元陣列緩衝區已經使用的字元數量
            for (int i = 0; i < charBufferLength - charBufferFree; i++)
            {
                sb.Append(charBuffer[i]);
            }
            string html = sb.ToString();
            return html;
        }
    }