首页  ·  知识 ·  
Label
      编辑:  图片来源:网络
a

不知您以前有没有想过要实现这样一种功能,在每个页面加一段指定代码或者是替换里面的什么东东,通过以下方式可以实现。

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();
    }
}

本文作者:网友 来源:网络收集
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读
也许感兴趣的
我们推荐的
主题最新
看看其它的
收藏至微信