最近无事写了一个HttpModule的抽象基类,从这个类继承的HttpModule就能直接对Page对象进行操作,只需要重写OnPrePageExecute方法,然后向该类提供的Page属性注册事件或者修改属性即可。
使用HttpModule是比页面基类更好的方法,更加灵活方便。
1
/**/
/// <summary>
2
/// 控制aspx页面的HttpModule通用基类
3
/// </summary>
4
public
abstract
class
PageModule : IHttpModule
5![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
6![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
7
private HttpApplication _application;
8![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
9![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
10
/// 销毁PageModule实例。
11
/// </summary>
12
public void Dispose()
13![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
14
}
15![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
16![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
17
/// 实现IHttpModule,初始化PageModule
18
/// </summary>
19
/// <param name="context">HttpApplication实例</param>
20
public void Init( HttpApplication context )
21![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
22
_application = context;
23![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
24
//_application.PostMapRequestHandler += new EventHandler( OnPostMapRequestHandler );
25
_application.PreRequestHandlerExecute += new EventHandler( OnPreRequestHandlerExecute );
26
_application.PostRequestHandlerExecute += new EventHandler( OnPostRequestHandlerExecute );
27![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
28
}
29![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
30![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
31
32
private void OnPreRequestHandlerExecute( object sender, EventArgs e )
33![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
34
_page = Context.Handler as System.Web.UI.Page;
35![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
36
if ( _page != null )
37
OnPrePageExecute( sender, e );
38![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
39
}
40![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
41![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
42
/// 当执行页面处理程序之前发生
43
/// </summary>
44
/// <param name="sender">事件源</param>
45
/// <param name="e">事件参数</param>
46
protected virtual void OnPrePageExecute( object sender, EventArgs e )
47![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
48![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
49
}
50![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
51
52
private void OnPostRequestHandlerExecute( object sender, EventArgs e )
53![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
54
if ( _page != null )
55
OnPostPageExecute( sender, e );
56
}
57![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
58![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
59
/// 当执行页面处理程序执行完毕时发生
60
/// </summary>
61
/// <param name="sender">事件源</param>
62
/// <param name="e">事件参数</param>
63
protected virtual void OnPostPageExecute( object sender, EventArgs e )
64![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
65![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
66
}
67![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
68![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
69![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
70![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
71
private System.Web.UI.Page _page;
72![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
73![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
74
/// 获取当前请求的页面对象
75
/// </summary>
76
protected System.Web.UI.Page Page
77![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
78![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _page; }
79
}
80![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
81![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
82
/// 获取当前的请求的Http上下文信息
83
/// </summary>
84
protected HttpContext Context
85![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
86![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _application.Context; }
87
}
88![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
89![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
90
/// 获取模块所在的HttpApplication实例
91
/// </summary>
92
protected HttpApplication ApplicationInstanse
93![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
94![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _application; }
95
}
96![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
97
}
98
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
2
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
3
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
4
![None.gif](/Images/OutliningIndicators/None.gif)
5
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
6
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
7
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
8
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
9
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
10
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
11
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
12
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
13
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
14
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
15
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
16
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
17
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
18
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
19
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
20
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
21
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
22
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
23
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
24
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
25
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
26
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
27
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
28
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
29
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
30
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
31
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
32
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
33
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
34
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
35
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
36
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
37
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
38
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
39
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
40
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
41
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
42
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
43
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
44
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
45
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
46
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
47
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
48
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
49
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
50
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
51
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
52
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
53
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
54
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
55
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
56
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
57
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
58
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
59
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
60
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
61
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
62
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
63
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
64
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
65
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
66
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
67
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
68
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
69
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
70
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
71
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
72
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
73
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
74
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
75
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
76
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
77
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
78
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
79
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
80
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
81
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
82
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
83
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
84
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
85
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
86
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
87
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
88
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
89
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
90
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
91
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
92
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
93
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
94
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
95
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
96
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
97
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
98
![None.gif](/Images/OutliningIndicators/None.gif)