用HttpModule控制或修改页面。


最近无事写了一个HttpModule的抽象基类,从这个类继承的HttpModule就能直接对Page对象进行操作,只需要重写OnPrePageExecute方法,然后向该类提供的Page属性注册事件或者修改属性即可。

使用HttpModule是比页面基类更好的方法,更加灵活方便。


 1 ExpandedBlockStart.gif ContractedBlock.gif    /**/ /// <summary>
 2InBlock.gif  /// 控制aspx页面的HttpModule通用基类
 3ExpandedBlockEnd.gif  /// </summary>

 4 None.gif    public   abstract   class  PageModule : IHttpModule
 5 ExpandedBlockStart.gifContractedBlock.gif   dot.gif {
 6InBlock.gif
 7InBlock.gif    private HttpApplication _application;
 8InBlock.gif
 9ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
10InBlock.gif    /// 销毁PageModule实例。
11ExpandedSubBlockEnd.gif    /// </summary>

12InBlock.gif    public void Dispose()
13ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
14ExpandedSubBlockEnd.gif    }

15InBlock.gif
16ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
17InBlock.gif    /// 实现IHttpModule,初始化PageModule
18InBlock.gif    /// </summary>
19ExpandedSubBlockEnd.gif    /// <param name="context">HttpApplication实例</param>

20InBlock.gif    public void Init( HttpApplication context )
21ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
22InBlock.gif      _application = context;
23InBlock.gif
24InBlock.gif      //_application.PostMapRequestHandler += new EventHandler( OnPostMapRequestHandler );
25InBlock.gif      _application.PreRequestHandlerExecute += new EventHandler( OnPreRequestHandlerExecute );
26InBlock.gif      _application.PostRequestHandlerExecute += new EventHandler( OnPostRequestHandlerExecute );
27InBlock.gif
28ExpandedSubBlockEnd.gif    }

29InBlock.gif
30InBlock.gif
31InBlock.gif    
32InBlock.gif    private void OnPreRequestHandlerExecute( object sender, EventArgs e )
33ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
34InBlock.gif      _page = Context.Handler as System.Web.UI.Page;
35InBlock.gif
36InBlock.gif      if ( _page != null )
37InBlock.gif        OnPrePageExecute( sender, e );
38InBlock.gif
39ExpandedSubBlockEnd.gif    }

40InBlock.gif
41ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
42InBlock.gif    /// 当执行页面处理程序之前发生
43InBlock.gif    /// </summary>
44InBlock.gif    /// <param name="sender">事件源</param>
45ExpandedSubBlockEnd.gif    /// <param name="e">事件参数</param>

46InBlock.gif    protected virtual void OnPrePageExecute( object sender, EventArgs e )
47ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif
48InBlock.gif
49ExpandedSubBlockEnd.gif    }

50InBlock.gif
51InBlock.gif    
52InBlock.gif    private void OnPostRequestHandlerExecute( object sender, EventArgs e )
53ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
54InBlock.gif      if ( _page != null )
55InBlock.gif        OnPostPageExecute( sender, e );
56ExpandedSubBlockEnd.gif    }

57InBlock.gif
58ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
59InBlock.gif    /// 当执行页面处理程序执行完毕时发生
60InBlock.gif    /// </summary>
61InBlock.gif    /// <param name="sender">事件源</param>
62ExpandedSubBlockEnd.gif    /// <param name="e">事件参数</param>

63InBlock.gif    protected virtual void OnPostPageExecute( object sender, EventArgs e )
64ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
65InBlock.gif
66ExpandedSubBlockEnd.gif    }

67InBlock.gif
68InBlock.gif
69InBlock.gif
70InBlock.gif
71InBlock.gif    private System.Web.UI.Page _page;
72InBlock.gif
73ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
74InBlock.gif    /// 获取当前请求的页面对象
75ExpandedSubBlockEnd.gif    /// </summary>

76InBlock.gif    protected System.Web.UI.Page Page
77ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
78ExpandedSubBlockStart.gifContractedSubBlock.gif      get dot.gifreturn _page; }
79ExpandedSubBlockEnd.gif    }

80InBlock.gif
81ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
82InBlock.gif    /// 获取当前的请求的Http上下文信息
83ExpandedSubBlockEnd.gif    /// </summary>

84InBlock.gif    protected HttpContext Context
85ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
86ExpandedSubBlockStart.gifContractedSubBlock.gif      get dot.gifreturn _application.Context; }
87ExpandedSubBlockEnd.gif    }

88InBlock.gif
89ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
90InBlock.gif    /// 获取模块所在的HttpApplication实例
91ExpandedSubBlockEnd.gif    /// </summary>

92InBlock.gif    protected HttpApplication ApplicationInstanse
93ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
94ExpandedSubBlockStart.gifContractedSubBlock.gif      get dot.gifreturn _application; }
95ExpandedSubBlockEnd.gif    }

96InBlock.gif
97ExpandedBlockEnd.gif  }

98 None.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值