日期:2014-05-17 浏览次数:20391 次
public class MyHttpModule : IHttpModule { private HttpApplication _contextApplication; private TextWriter tw_new, tw_old; private StringBuilder _content; private FieldInfo fileinfo; public void Dispose() { _contextApplication = null; _contextApplication.Dispose(); } public void Init(HttpApplication context) { _contextApplication = context; _contextApplication.PreRequestHandlerExecute += new EventHandler(_contextApplication_PreRequestHandlerExecute); } private void _contextApplication_PreRequestHandlerExecute(object sender, EventArgs e) { HttpContext context = _contextApplication.Context; Page _page = context.Handler as Page; _page.Unload += new EventHandler(_page_Unload); _content = new StringBuilder(); tw_old = context.Response.Output;//Response原来的OutPut tw_new = new StringWriter(_content); Type type_rp = context.Response.GetType(); fileinfo = type_rp.GetField("_writer", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); fileinfo.SetValue(context.Response, tw_new); } void _page_Unload(object sender, EventArgs e) { fileinfo.SetValue(HttpContext.Current.Response, tw_old); _content.AppendLine("胡学成2B"); HttpContext.Current.Response.Write(_content.ToString()); }