一点一点学ASP.NET之基础概念——HttpModule
原文地址为: 一点一点学ASP.NET之基础概念——HttpModule
HttpModule
—— 一点一点学ASP.NET
文野: 2006 年 8 月 9 日 星期三
上一篇:一点一点学ASP.NET之基础——HTTP运行期与页面执行模型
HttpModule是如何工作的
当一个HTTP请求到达HttpModule时,整个ASP.NET Framework系统还并没有对这个HTTP请求做任何处理,也就是说此时对于HTTP请求来讲,HttpModule是一个HTTP请求的“必经之路”,所以可以在这个HTTP请求传递到真正的请求处理中心(HttpHandler)之前附加一些需要的信息在这个HTTP请求信息之上,或者针对截获的这个HTTP请求信息作一些额外的工作,或者在某些情况下干脆终止满足一些条件的HTTP请求,从而可以起到一个Filter过滤器的作用。
示例1:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
namespace MyHttpModule
{
/// <summary>
/// 说明:用来实现自己的HttpModule类。
/// 作者:文野
/// 联系:stwyhm@cnblogs.com
/// </summary>
public class MyFirstHttpModule : IHttpModule
{
private void Application_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
HttpRequest request = application.Request;
HttpResponse response = application.Response;
response.Write("我来自自定义HttpModule中的BeginRequest<br />");
}
private void Application_EndRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
HttpRequest request = application.Request;
HttpResponse response = application.Response;
response.Write("我来自自定义HttpModule中的EndRequest<br />");
}
#region IHttpModule 成员
public void Dispose()
{}
public void Init(HttpApplication application)
{
application.BeginRequest += new EventHandler(Application_BeginRequest);
application.EndRequest += new EventHandler(Application_EndRequest);
}
#endregion
}
}
在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生命周期
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
namespace MyHttpModule
{
public class ValidaterHttpModule : IHttpModule
{
#region IHttpModule 成员
public void Dispose()
{}
public void Init(HttpApplication application)
{
application.BeginRequest += new EventHandler(application_BeginRequest);
application.EndRequest += new EventHandler(application_EndRequest);
application.PreRequestHandlerExecute += new EventHandler(application_PreRequestHandlerExecute);
application.PostRequestHandlerExecute += new EventHandler(application_PostRequestHandlerExecute);
application.ReleaseRequestState += new EventHandler(application_ReleaseRequestState);
application.AcquireRequestState += new EventHandler(application_AcquireRequestState);
application.AuthenticateRequest += new EventHandler(application_AuthenticateRequest);
application.AuthorizeRequest += new EventHandler(application_AuthorizeRequest);
application.ResolveRequestCache += new EventHandler(application_ResolveRequestCache);
application.PreSendRequestHeaders += new EventHandler(application_PreSendRequestHeaders);
application.PreSendRequestContent += new EventHandler(application_PreSendRequestContent);
}
void application_PreSendRequestContent(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
application.Context.Response.Write("application_PreSendRequestContent<br/>");
}
void application_PreSendRequestHeaders(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
application.Context.Response.Write("application_PreSendRequestHeaders<br/>");
}
void application_ResolveRequestCache(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
application.Context.Response.Write("application_ResolveRequestCache<br/>");
}
void application_AuthorizeRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
application.Context.Response.Write("application_AuthorizeRequest<br/>");
}
void application_AuthenticateRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
application.Context.Response.Write("application_AuthenticateRequest<br/>");
}
void application_AcquireRequestState(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
application.Context.Response.Write("application_AcquireRequestState<br/>");
}
void application_ReleaseRequestState(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
application.Context.Response.Write("application_ReleaseRequestState<br/>");
}
void application_PostRequestHandlerExecute(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
application.Context.Response.Write("application_PostRequestHandlerExecute<br/>");
}
void application_PreRequestHandlerExecute(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
application.Context.Response.Write("application_PreRequestHandlerExecute<br/>");
}
void application_EndRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
application.Context.Response.Write("application_EndRequest<br/>");
}
void application_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
application.Context.Response.Write("application_BeginRequest<br/>");
}
#endregion
}
}
多个自定义的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:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
namespace MyHttpModule
{
public class CompleteRequestHttpModule : IHttpModule
{
#region IHttpModule 成员
public void Dispose()
{}
public void Init(HttpApplication application)
{
application.BeginRequest += new EventHandler(Application_BeginRequest);
}
void Application_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
application.CompleteRequest();
application.Context.Response.Write("请求被终止。");
}
#endregion
}
}
参考资料
《ASP.NET深入解析》
《ASP.NET实用全书》
本文源码:
下载
下一篇:一点一点学ASP.NET之基础概念——HttpHandler
转载请注明本文地址: 一点一点学ASP.NET之基础概念——HttpModule
一点一点学ASP.NET之基础概念——HttpModule相关推荐
- 一点一点学ASP.NET之基础概念——委托
委托的概念 委托实际上是类(一个貌似函数一样的类),我们已经使用函数指针很多年了--函数指针也被称为过程类型,但是它们的实现都不是类.它们是单独的函数指针的简单实例.委托是包含这些功能的类,委托类通过 ...
- 一点一点学ASP.NET之基础概念——HTTP运行期与页面执行模型
转自:http://www.cnblogs.com/stwyhm/archive/2006/08/08/470972.aspx HTTP运行期 HTTP运行期处理客户端应用程序(例如Web浏览器)进入 ...
- 一点一点学ASP.NET之基础概念——HttpHandler
概述 HttpHandler是一个HTTP请求的真正处理中心,也正是在这个HttpHandler容器中,ASP.NET Framework才真正地对客户端请求的服务器页面做出编译和执行,并将处理过后的 ...
- 一起学AI:核心基础概念
一起学AI:核心基础概念 AI基础概念 什么是学习率? batchsize和epoch分别是什么? 梯度消失与梯度爆炸是什么? 什么是过拟合.欠拟合和泛化? 归一化.正则化.标准化是什么? 线性回归和 ...
- 从零开始学ASP.NET(基础篇)
原作者:蓝鲸 出处:5D多媒体 学ASPNET与ASP有区别,这种区别不是语言上的,而是思路上的区别.ASP是纯面向过程的,而ASPNET是完全面向对向的.这种区别使我们在编程的结构设计上要与ASP有 ...
- 回头再学Asp.net系列--基础篇(一)
Web Page的生命周期与Postback 参考 http://www.cnblogs.com/artech/archive/2007/04/06/702658.html 了解ASP.NET Web ...
- 回头再学Asp.net系列--基础篇(六)
随着面向对象分析与设计的发展,一个良好的设计,核心原则之一就是将变化隔离,使得变化部分发生变化时,不变部分不受影响(这也是OCP的目的).为了做到这一点,要利用面向对象中的多态性,使用多态性后,客户类 ...
- java u003_我在B站学编程 DAY-003 JAVA基础概念和语法
八进制:0-7组成 例子: 八卦 指令在机器中以机器码格式输出,机器码一次原始输出位数是八的倍数,即以字节长度为最小单位输出. 十六进制:0-9,abcde...组成,0X开头 十六进制可以更紧凑地表 ...
- 数据库基础笔记(MySQL)1 —— 基础概念
(写算法题目把自己心态搞崩了,休息两天顺便把之前学的数据库手写笔记记录在网上方便查阅,因为只是自己学的时候做的笔记,如果有错误欢迎大家指正,过两天再重回算法的怀抱哈哈) 如何下载配置 这个感觉视频讲的 ...
最新文章
- DSP专家给你一个选择FPGA的理由(让人看了热血沸腾的一篇科普文章)
- matlab训练神经网络模型并导入simulink详细步骤
- [詹兴致矩阵论习题参考解答]习题3.6
- 今日代码(200708)--缺失值处理
- 好久不来这里写东西了.
- 基于springboot的健身管理系统
- Android开发点滴 - 实现层级式导航(API 16+)
- 新手如何学习单片机,一套很好的51单片机教程
- 苹果越狱后怎么还原_iOS 软件证书失效怎么办?越狱后你可以这么做
- 解决阿里云FTP 227 Entering Passive Mode
- 应聘高校教师的试讲技巧
- 【bfs】密码锁-C++
- Linux应用开发(十一)——I^2C接口
- 3Com收购华为2%持股 有望控股双方合资公司
- python写txt方法_怎么实现python写入txt文件方法
- 通过串口号获得该串口号对应的设备信息(如设备范例ID等)
- 【Unity】VideoPlayer实现视频播放
- Java代码审计基础——RMI原理和反序列化利用链
- 读书笔记_卓越程序员密码
- 微星B550M迫击炮WiFi板第二条M2固态(M2_2接口)不识别问题