• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Hing
沉浸在技术的海洋,通过学习与讨论,只有不断的提高,才有可能去创造与实现.
博客园    首页    新随笔    联系   管理    订阅  订阅
一点一点学ASP.NET之基础概念——HttpModule
HttpModule是如何工作的

当一个HTTP请求到达HttpModule时,整个ASP.NET Framework系统还并没有对这个HTTP请求做任何处理,也就是说此时对于HTTP请求来讲,HttpModule是一个HTTP请求的“必经之路”,所以可以在这个HTTP请求传递到真正的请求处理中心(HttpHandler)之前附加一些需要的信息在这个HTTP请求信息之上,或者针对截获的这个HTTP请求信息作一些额外的工作,或者在某些情况下干脆终止满足一些条件的HTTP请求,从而可以起到一个Filter过滤器的作用。

示例1:


Code
 1using System;
 2
 3using System.Collections.Generic;
 4
 5using System.Text;
 6
 7using System.Web;
 8
 9 
10
11namespace MyHttpModule
12
13{
14
15     /**//// <summary>
16
17     /// 说明:用来实现自己的HttpModule类。
18
19     /// 作者:文野
20
21     /// 联系:stwyhm@cnblogs.com
22
23     /// </summary>

24
25     public class MyFirstHttpModule : IHttpModule
26
27     {
28
29         private void Application_BeginRequest(object sender, EventArgs e)
30
31         {
32
33              HttpApplication application = (HttpApplication)sender;
34
35              HttpContext context = application.Context;
36
37              HttpRequest request = application.Request;
38
39              HttpResponse response = application.Response;
40
41 
42
43              response.Write("我来自自定义HttpModule中的BeginRequest<br />");
44
45         }

46
47 
48
49         private void Application_EndRequest(object sender, EventArgs e)
50
51         {
52
53              HttpApplication application = (HttpApplication)sender;
54
55              HttpContext context = application.Context;
56
57              HttpRequest request = application.Request;
58
59              HttpResponse response = application.Response;
60
61 
62
63              response.Write("我来自自定义HttpModule中的EndRequest<br />");
64
65         }

66
67 
68
69         IHttpModule 成员#region IHttpModule 成员
70
71 
72
73         public void Dispose()
74
75         {}
76
77 
78
79         public void Init(HttpApplication application)
80
81         {
82
83              application.BeginRequest += new EventHandler(Application_BeginRequest);
84
85              application.EndRequest += new EventHandler(Application_EndRequest);
86
87         }

88
89 
90
91         #endregion

92
93     }

94
95}

在Web.config进行如下配置

<add name="MyFirstHttpModule" type="MyHttpModule.MyFirstHttpModule,MyHttpModule"/>


 


深入了解
HttpModule

一个HTTP请求在HttpModule容器的传递过程中,会在某一时刻(ResolveRequestCache事件)将这个HTTP请求传递给HttpHandler容器。在这个事件之后,HttpModule容器会建立一个HttpHandler的入口实例,但是此时并没有将HTTP请求控制权交出,而是继续触发AcquireRequestState事件以及PreRequestHandlerExcute事件。在PreRequestHandlerExcute事件之后,HttpModule窗口就会将控制权暂时交给HttpHandler容器,以便进行真正的HTTP请求处理工作。

而在HttpHandler容器内部会执行ProcessRequest方法来处理HTTP请求。在容器HttpHandler处理完毕整个HTTP请求之后,会将控制权交还给HttpModule,HttpModule则会继续对处理完毕的HTTP请求信息流进行层层的转交动作,直到返回到客户端为止。


图
1:HttpModule生命周期示意图

 

示例2:验证HttpModule生命周期


Code
  1using System;
  2
  3using System.Collections.Generic;
  4
  5using System.Text;
  6
  7using System.Web;
  8
  9 
 10
 11namespace MyHttpModule
 12
 13{
 14
 15    public class ValidaterHttpModule : IHttpModule
 16
 17    {
 18
 19        IHttpModule 成员#region IHttpModule 成员
 20
 21 
 22
 23        public void Dispose()
 24
 25        {}
 26
 27 
 28
 29        public void Init(HttpApplication application)
 30
 31        {
 32
 33            application.BeginRequest += new EventHandler(application_BeginRequest);
 34
 35            application.EndRequest += new EventHandler(application_EndRequest);
 36
 37            application.PreRequestHandlerExecute += new EventHandler(application_PreRequestHandlerExecute);
 38
 39            application.PostRequestHandlerExecute += new EventHandler(application_PostRequestHandlerExecute);
 40
 41            application.ReleaseRequestState += new EventHandler(application_ReleaseRequestState);
 42
 43            application.AcquireRequestState += new EventHandler(application_AcquireRequestState);
 44
 45            application.AuthenticateRequest += new EventHandler(application_AuthenticateRequest);
 46
 47            application.AuthorizeRequest += new EventHandler(application_AuthorizeRequest);
 48
 49            application.ResolveRequestCache += new EventHandler(application_ResolveRequestCache);
 50
 51            application.PreSendRequestHeaders += new EventHandler(application_PreSendRequestHeaders);
 52
 53            application.PreSendRequestContent += new EventHandler(application_PreSendRequestContent);
 54
 55        }

 56
 57 
 58
 59        void application_PreSendRequestContent(object sender, EventArgs e)
 60
 61        {
 62
 63            HttpApplication application = (HttpApplication)sender;
 64
 65           application.Context.Response.Write("application_PreSendRequestContent<br/>");
 66
 67        }

 68
 69 
 70
 71        void application_PreSendRequestHeaders(object sender, EventArgs e)
 72
 73        {
 74
 75            HttpApplication application = (HttpApplication)sender;
 76
 77            application.Context.Response.Write("application_PreSendRequestHeaders<br/>");
 78
 79        }

 80
 81 
 82
 83        void application_ResolveRequestCache(object sender, EventArgs e)
 84
 85        {
 86
 87            HttpApplication application = (HttpApplication)sender;
 88
 89            application.Context.Response.Write("application_ResolveRequestCache<br/>");
 90
 91        }

 92
 93 
 94
 95        void application_AuthorizeRequest(object sender, EventArgs e)
 96
 97        {
 98
 99            HttpApplication application = (HttpApplication)sender;
100
101            application.Context.Response.Write("application_AuthorizeRequest<br/>");
102
103        }

104
105 
106
107        void application_AuthenticateRequest(object sender, EventArgs e)
108
109        {
110
111            HttpApplication application = (HttpApplication)sender;
112
113            application.Context.Response.Write("application_AuthenticateRequest<br/>");
114
115        }

116
117 
118
119        void application_AcquireRequestState(object sender, EventArgs e)
120
121        {
122
123            HttpApplication application = (HttpApplication)sender;
124
125            application.Context.Response.Write("application_AcquireRequestState<br/>");
126
127        }

128
129 
130
131        void application_ReleaseRequestState(object sender, EventArgs e)
132
133        {
134
135            HttpApplication application = (HttpApplication)sender;
136
137            application.Context.Response.Write("application_ReleaseRequestState<br/>");
138
139        }

140
141 
142
143        void application_PostRequestHandlerExecute(object sender, EventArgs e)
144
145        {
146
147            HttpApplication application = (HttpApplication)sender;
148
149            application.Context.Response.Write("application_PostRequestHandlerExecute<br/>");
150
151        }

152
153 
154
155        void application_PreRequestHandlerExecute(object sender, EventArgs e)
156
157        {
158
159            HttpApplication application = (HttpApplication)sender;
160
161            application.Context.Response.Write("application_PreRequestHandlerExecute<br/>");
162
163        }

164
165 
166
167        void application_EndRequest(object sender, EventArgs e)
168
169        {
170
171            HttpApplication application = (HttpApplication)sender;
172
173            application.Context.Response.Write("application_EndRequest<br/>");
174
175        }

176
177 
178
179        void application_BeginRequest(object sender, EventArgs e)
180
181        {
182
183            HttpApplication application = (HttpApplication)sender;
184
185            application.Context.Response.Write("application_BeginRequest<br/>");
186
187        }

188
189 
190
191       
192
193 
194
195        #endregion

196
197    }

198
199}


 

多个自定义的Http Module的运作

从运行结果可以看到,在web.config文件中引入自定义HttpModule的顺序就决定了多个自定义HttpModule在处理一个HTTP请求的接管顺序。注:系统默认那几个HttpModule是最先衩ASP.NET Framework所加载上去的。

示例3:(代码类同示例2)


 


在
HttpModule中终止此次的HTTP请求

可以利用HttpModule通过调用HttpApplication.CompleteRequest()方法实现当满足某一个条件时终止此次的HTTP请求。

需要注意的是,即使调用了HttpApplication.CompleteRequest()方法终止了一个HTTP请求,ASP.NET Framework仍然会触发HttpApplication后面的这3个事件:EndRequest事件、PreSendRequestHeaders事件、PreSendRequestContent事件。

如果存在多个自定义的HttpModule的话,当Module1终止了一个HTTP请求,这个HTTP请求将不会再触发Module2中相应的事件了,但Module2的最后三个事件仍会被触发。

示例4:


Code
 1using System;
 2
 3using System.Collections.Generic;
 4
 5using System.Text;
 6
 7using System.Web;
 8
 9 
10
11namespace MyHttpModule
12
13{
14
15    public class CompleteRequestHttpModule : IHttpModule
16
17    {
18
19        IHttpModule 成员#region IHttpModule 成员
20
21 
22
23        public void Dispose()
24
25        {}
26
27 
28
29        public void Init(HttpApplication application)
30
31        {
32
33            application.BeginRequest += new EventHandler(Application_BeginRequest);
34
35        }

36
37 
38
39        void Application_BeginRequest(object sender, EventArgs e)
40
41        {
42
43            HttpApplication application = (HttpApplication)sender;
44
45            application.CompleteRequest();
46
47            application.Context.Response.Write("请求被终止。");
48
49        }

50
51 
52
53        #endregion

54
55    }

56
57}


 

参考资料

《ASP.NET深入解析》

《ASP.NET实用全书》

 
本文源码:
   
下载

posted on 2009-03-30 15:39  Pacer  阅读(208)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3