阻拦asp.net输出流并进行处置的办法

这篇文章实例首要完结对已经生成了HTML的页面做一些输出到客户端之前的处置。


  办法的完结原理百家乐e78是:把Response的输出重定向到自定义的容器内,也即是咱们的StringBuilder目标里,在HTML一切的向页面输出都变成了向StringBuilder输出,然后咱们对StringBuilder处置完结之后,再把Response的输出重定向到本来的页面上,然后再经过Response.Write办法把StringBuilder的内容输出到页面上。


  这儿之所以用反射,是因为Response目标的OutPut特点是只读的,经过反编译该类的程序集发现,OutPut实际上是内部私有成员 _writer来完结输出的。因而经过反射来改写该成员的值以完结输出流的重定向。


  具体功用代码如下:


  view sourceprint?01 using System;


  02 using System.Collections.Generic;


  03 using System.Linq;


  04 using System.Web;


  05 using System.Web.UI;


  06 using System.Web.UI.WebControls;


  07 using System.Text;


  08 using System.IO;


  09 using System.Reflection;


  10 public partial class _Default : System.Web.UI.Page


  11 {


  12 StringBuilder content = new StringBuilder();


  13 TextWriter tw_old, tw_new;


  14 FieldInfo tw_field;


  15 protected void Page_Load(object sender, EventArgs e)


  16 {


  17 var context = HttpContext.Current;


  18


  19 tw_old = context.Response.Output;//Response本来的OutPut


  20 tw_new = new StringWriter(content);//一个StringWriter,用来获取页面内容


  21 var type_rp = context.Response.GetType();


  22 //经过反射获取目标的私有字段


  23 tw_field = type_rp.GetField("_writer", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);


  24 tw_field.SetValue(context.Response, tw_new);


  25 }


  26 protected override void Render(HtmlTextWriter writer)


  27 {


  28 base.Render(writer);


  29 //替换回Response的OutPut


  30 tw_field.SetValue(HttpContext.Current.Response, tw_old);


  31 //做自个的处置


  32 content.AppendLine("");


  33 HttpContext.Current.Response.Write(content.ToString());


  34 }


  35 }


  办法二,用HttpModul来完结:


  view sourceprint?01 using System;


  02 using System.Collections.Generic;


  03 using System.Linq;


  04 using System.Web;


  05 using System.Web.UI;


  06 using System.IO;


  07 using System.Text;


  08 using System.Reflection;


  09 ///


  10 ///HttpModule 的摘要说明


  11 ///


  12 public class HttpModule : IHttpModule


  13 {


  14 private HttpApplication _contextApplication;


  15 private TextWriter tw_new, tw_old;


  16 private StringBuilder _content;


  17 private FieldInfo tw_field;


  18 public void Init(HttpApplication context)


  19 {


  20 _contextApplication = context;


  21 _contextApplication.PreRequestHandlerExecute += new EventHandler(_contextApplication_PreRequestHandlerExecute);


  22 }


  23 public void Dispose()


  24 {


  25 _contextApplication = null;


  26 _contextApplication.Dispose();


  27 }


  28 public void _contextApplication_PreRequestHandlerExecute(object sender, EventArgs e)


  29 {


  30 HttpContext context = _contextApplication.Context;


  31


  32 var _page = context.Handler as System.Web.UI.Page;


  33 _page.Unload += new EventHandler(_page_Unload);


  34


  35 _content = new StringBuilder();


  36 tw_old = context.Response.Output;//Response本来的OutPut


  37 tw_new = new StringWriter(_content);//一个StringWriter,用来获取页面内容


  38 var type_rp = context.Response.GetType();


  39 tw_field = type_rp.GetField("_writer", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);


  40 tw_field.SetValue(context.Response, tw_new);


  41 }


  42 void _page_Unload(object sender, EventArgs e)


  43 {


  44 //替换回Response的OutPut


  45 tw_field.SetValue(HttpContext.Current.Response, tw_old);


  46 //做自个的处置


  47 _content.AppendLine("");


  48 HttpContext.Current.Response.Write(_content.ToString());


  49 }


  50 }


  办法三:


  view sourceprint?01 public class HttpModule : IHttpModule


  02 {


  03 private HttpApplication _contextApplication;


  04 private TextWriter tw_new, tw_old;


  05 private StringBuilder _content;


  06 private FieldInfo tw_field;


  07 public void Init(HttpApplication application)


  08 {


  09 _contextApplication = application;


  10 _contextApplication.BeginRequest += new EventHandler(_contextApplication_BeginRequest);


  11 _contextApplication.EndRequest +=new EventHandler(_contextApplication_EndRequest);


  12 }


  13 void _contextApplication_BeginRequest(object sender, EventArgs e)


  14 {


  15 _content = new StringBuilder();


  16 tw_old = _contextApplication.Response.Output;


  17 tw_new = new StringWriter(_content);


  18 var type_rp = _contextApplication.Response.GetType();


  19 tw_field = type_rp.GetField("_writer", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);


  20 tw_field.SetValue(_contextApplication.Response, tw_new);


  21 }


  22 void _contextApplication_EndRequest(object sender, EventArgs e)


  23 {


  24 tw_field.SetValue(_contextApplication.Response, tw_old);


  25 //做自个的处置


  26 _content.AppendLine("");


  27 _contextApplication.Response.Write(_content.ToString());


  28 }


  29 public void Dispose()


  30 {


  31 _contextApplication = null;


  32 _contextApplication.Dispose();


  33 }


  34 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值