zoukankan      html  css  js  c++  java
  • 拦截asp.net输出流做处理

    本文标题是指对已经生成了HTML的页面做一些输出到客户端之前的处理。

    方法的原理是:把Response的输出重定向到自定义的容器内,也就是我们的StringBuilder对象里,在HTML所有的向页面输出都变成了向StringBuilder输出,然后我们对StringBuilder处理完成之后,再把Response的输出重定向到原来的页面上,然后再通过Response.Write方法把StringBuilder的内容输出到页面上

     这里之所以用反射,是因为Response对象的OutPut属性是只读的,通过反编译该类的程序集发现,OutPut实际上是内部私有成员 _writer来实现输出的。因此通过反射来改写该成员的值以实现输出流的重定向。

    [c-sharp] view plaincopy
     
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using System.Web.UI;  
    6. using System.Web.UI.WebControls;  
    7. using System.Text;  
    8. using System.IO;  
    9. using System.Reflection;  
    10.   
    11. public partial class _Default : System.Web.UI.Page   
    12. {  
    13.     StringBuilder content = new StringBuilder();  
    14.     TextWriter tw_old, tw_new;  
    15.     FieldInfo tw_field;  
    16.   
    17.     protected void Page_Load(object sender, EventArgs e)  
    18.     {  
    19.         var context = HttpContext.Current;  
    20.   
    21.         tw_old = context.Response.Output;//Response原来的OutPut  
    22.         tw_new = new StringWriter(content);//一个StringWriter,用来获取页面内容  
    23.         var type_rp = context.Response.GetType();  
    24.         //通过反射获取对象的私有字段  
    25.         tw_field = type_rp.GetField("_writer", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);  
    26.         tw_field.SetValue(context.Response, tw_new);  
    27.     }  
    28.   
    29.     protected override void Render(HtmlTextWriter writer)  
    30.     {  
    31.         base.Render(writer);  
    32.         //替换回Response的OutPut  
    33.         tw_field.SetValue(HttpContext.Current.Response, tw_old);  
    34.         //做自己的处理  
    35.         content.AppendLine("<!--江湖小子-->");  
    36.         HttpContext.Current.Response.Write(content.ToString());  
    37.     }  
    38. }  
    39.   
    40. 方法二,用HttpModul来实现:  
    41.   
    42. using System;  
    43. using System.Collections.Generic;  
    44. using System.Linq;  
    45. using System.Web;  
    46. using System.Web.UI;  
    47. using System.IO;  
    48. using System.Text;  
    49. using System.Reflection;  
    50.   
    51. /// <summary>  
    52. ///HttpModule 的摘要说明  
    53. /// </summary>  
    54. public class HttpModule : IHttpModule  
    55. {  
    56.     private HttpApplication _contextApplication;  
    57.     private TextWriter tw_new, tw_old;  
    58.     private StringBuilder _content;  
    59.     private FieldInfo tw_field;  
    60.   
    61.     public void Init(HttpApplication context)  
    62.     {  
    63.         _contextApplication = context;  
    64.         _contextApplication.PreRequestHandlerExecute += new EventHandler(_contextApplication_PreRequestHandlerExecute);  
    65.     }  
    66.   
    67.     public void Dispose()  
    68.     {  
    69.         _contextApplication = null;  
    70.         _contextApplication.Dispose();  
    71.     }  
    72.   
    73.     public void _contextApplication_PreRequestHandlerExecute(object sender, EventArgs e)  
    74.     {  
    75.         HttpContext context = _contextApplication.Context;  
    76.   
    77.         var _page = context.Handler as System.Web.UI.Page;  
    78.         _page.Unload += new EventHandler(_page_Unload);  
    79.   
    80.         _content = new StringBuilder();  
    81.         tw_old = context.Response.Output;//Response原来的OutPut  
    82.         tw_new = new StringWriter(_content);//一个StringWriter,用来获取页面内容  
    83.         var type_rp = context.Response.GetType();  
    84.         tw_field = type_rp.GetField("_writer", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);  
    85.         tw_field.SetValue(context.Response, tw_new);  
    86.     }  
    87.   
    88.   
    89.     void _page_Unload(object sender, EventArgs e)  
    90.     {  
    91.         //替换回Response的OutPut  
    92.         tw_field.SetValue(HttpContext.Current.Response, tw_old);  
    93.         //做自己的处理  
    94.         _content.AppendLine("<!--江湖小子-->");  
    95.         HttpContext.Current.Response.Write(_content.ToString());  
    96.     }  
    97.   
    98. }  
    99.   
    100. 方法三:  
    101. public class HttpModule : IHttpModule  
    102. {  
    103.     private HttpApplication _contextApplication;  
    104.     private TextWriter tw_new, tw_old;  
    105.     private StringBuilder _content;  
    106.     private FieldInfo tw_field;  
    107.   
    108.     public void Init(HttpApplication application)  
    109.     {  
    110.         _contextApplication = application;  
    111.         _contextApplication.BeginRequest += new EventHandler(_contextApplication_BeginRequest);  
    112.         _contextApplication.EndRequest +=new EventHandler(_contextApplication_EndRequest);  
    113.     }  
    114.   
    115.     void _contextApplication_BeginRequest(object sender, EventArgs e)  
    116.     {  
    117.         _content = new StringBuilder();  
    118.         tw_old = _contextApplication.Response.Output;  
    119.         tw_new = new StringWriter(_content);  
    120.         var type_rp = _contextApplication.Response.GetType();  
    121.         tw_field = type_rp.GetField("_writer", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);  
    122.         tw_field.SetValue(_contextApplication.Response, tw_new);  
    123.     }  
    124.   
    125.     void _contextApplication_EndRequest(object sender, EventArgs e)  
    126.     {  
    127.         tw_field.SetValue(_contextApplication.Response, tw_old);  
    128.         //做自己的处理  
    129.         _content.AppendLine("<!--jhxz-->");  
    130.         _contextApplication.Response.Write(_content.ToString());  
    131.     }  
    132.   
    133.     public void Dispose()  
    134.     {  
    135.         _contextApplication = null;  
    136.         _contextApplication.Dispose();  
    137.     }  
    138. }  

     最后还是推荐一篇好文:码农欧洲出差的一点小插曲

  • 相关阅读:
    关于.net core https支持的整理
    数据库动态字段 个人设计的思考
    解决PowerDesigner中table的code长度限制和column的code长度限制
    Git内网服务器搭建
    解决.net core读取appSetting.json文件中文字符乱码
    excel 转换成pdf 总结
    .net生成荣誉证书
    Adobe Acrobat DC 安装
    swagger生成错误问题 汇总解决
    svn或git 提交文件排除
  • 原文地址:https://www.cnblogs.com/ranran/p/3935016.html
Copyright © 2011-2022 走看看