拦截asp.net输出流并进行处理的方法
程序员文章站
2024-02-24 17:01:10
本文实例主要实现对已经生成了html的页面做一些输出到客户端之前的处理。
方法的实现原理是:把response的输出重定向到自定义的容器内,也就是我们的stringbui...
本文实例主要实现对已经生成了html的页面做一些输出到客户端之前的处理。
方法的实现原理是:把response的输出重定向到自定义的容器内,也就是我们的stringbuilder对象里,在html所有的向页面输出都变成了向stringbuilder输出,然后我们对stringbuilder处理完成之后,再把response的输出重定向到原来的页面上,然后再通过response.write方法把stringbuilder的内容输出到页面上。
这里之所以用反射,是因为response对象的output属性是只读的,通过反编译该类的程序集发现,output实际上是内部私有成员 _writer来实现输出的。因此通过反射来改写该成员的值以实现输出流的重定向。
具体功能代码如下:
using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; using system.text; using system.io; using system.reflection; public partial class _default : system.web.ui.page { stringbuilder content = new stringbuilder(); textwriter tw_old, tw_new; fieldinfo tw_field; protected void page_load(object sender, eventargs e) { var context = httpcontext.current; tw_old = context.response.output;//response原来的output tw_new = new stringwriter(content);//一个stringwriter,用来获取页面内容 var type_rp = context.response.gettype(); //通过反射获取对象的私有字段 tw_field = type_rp.getfield("_writer", system.reflection.bindingflags.public | system.reflection.bindingflags.nonpublic | system.reflection.bindingflags.instance); tw_field.setvalue(context.response, tw_new); } protected override void render(htmltextwriter writer) { base.render(writer); //替换回response的output tw_field.setvalue(httpcontext.current.response, tw_old); //做自己的处理 content.appendline("<!--江湖小子-->"); httpcontext.current.response.write(content.tostring()); } }
方法二,用httpmodul来实现:
using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.io; using system.text; using system.reflection; /// <summary> ///httpmodule 的摘要说明 /// </summary> public class httpmodule : ihttpmodule { private httpapplication _contextapplication; private textwriter tw_new, tw_old; private stringbuilder _content; private fieldinfo tw_field; public void init(httpapplication context) { _contextapplication = context; _contextapplication.prerequesthandlerexecute += new eventhandler(_contextapplication_prerequesthandlerexecute); } public void dispose() { _contextapplication = null; _contextapplication.dispose(); } public void _contextapplication_prerequesthandlerexecute(object sender, eventargs e) { httpcontext context = _contextapplication.context; var _page = context.handler as system.web.ui.page; _page.unload += new eventhandler(_page_unload); _content = new stringbuilder(); tw_old = context.response.output;//response原来的output tw_new = new stringwriter(_content);//一个stringwriter,用来获取页面内容 var type_rp = context.response.gettype(); tw_field = type_rp.getfield("_writer", system.reflection.bindingflags.public | system.reflection.bindingflags.nonpublic | system.reflection.bindingflags.instance); tw_field.setvalue(context.response, tw_new); } void _page_unload(object sender, eventargs e) { //替换回response的output tw_field.setvalue(httpcontext.current.response, tw_old); //做自己的处理 _content.appendline("<!--江湖小子-->"); httpcontext.current.response.write(_content.tostring()); } }
方法三:
public class httpmodule : ihttpmodule { private httpapplication _contextapplication; private textwriter tw_new, tw_old; private stringbuilder _content; private fieldinfo tw_field; public void init(httpapplication application) { _contextapplication = application; _contextapplication.beginrequest += new eventhandler(_contextapplication_beginrequest); _contextapplication.endrequest +=new eventhandler(_contextapplication_endrequest); } void _contextapplication_beginrequest(object sender, eventargs e) { _content = new stringbuilder(); tw_old = _contextapplication.response.output; tw_new = new stringwriter(_content); var type_rp = _contextapplication.response.gettype(); tw_field = type_rp.getfield("_writer", system.reflection.bindingflags.public | system.reflection.bindingflags.nonpublic | system.reflection.bindingflags.instance); tw_field.setvalue(_contextapplication.response, tw_new); } void _contextapplication_endrequest(object sender, eventargs e) { tw_field.setvalue(_contextapplication.response, tw_old); //做自己的处理 _content.appendline("<!--jhxz-->"); _contextapplication.response.write(_content.tostring()); } public void dispose() { _contextapplication = null; _contextapplication.dispose(); } }
相信本文所述对大家的asp.net程序设计有一定的借鉴价值。