本文只是对HttpModule和HttpHandler做最初步的了解。非菜鸟级别人士可直接无视。
ASP.NET 中Http的请求流程
1. 用户发出的客户端请求达到服务器后,会被服务端的inetinfo.exe 进程捕获,该进程将该http请求转交给asp.net_isapi.dll进程,然后通过http pipeline 管道(具体这是什么东西我也不清楚)传送给aspnet_wp.exe进程来处理。接下来就到了.net framework的httpruntime处理中心,处理完毕后,就发送给用户的浏览器。
3. 大体的流程如下所示
HttpRequest------ inetinfo.exe--- aspnet_isapi.dll ---- http pipeline --aspnet_wp.exe ---http runtime----Http Application ---Http Application ---HttpModule---HttpHandler Factory ---HttpHandler -----HttpHandler.Process Request()
以上内容大多取自【我也想飞翔-tangself】的HttpModule的认识一文,大家可以 看原作: http://www.cnblogs.com/tangself/archive/2011/03/28/1998007.html
4. 执行ProcessRequest 的过程其实也就是整个ASP.NET page 页面的生命周期。
我们可以实现自己的HttpModule,只要继承IHttpModule即可。
IHttpModul接口如下
Using System;
Namespace System.Web
{
Public interface IHttpModule
{
// 销毁不再被HttpModule使用的资源
void Dispose();
// 初始化一个Module,为捕获HttpRequest做准备
void Init(HttpApplication context);
{}
}
下面是我自己写的Module,实际上和 http://www.cnblogs.com/tangself/archive/2011/03/28/1998007.html差不多。。。
public class MyHttpModule:IHttpModule
{
public MyHttpModule()
{
}
public void Dispose()
{
}
///
/// 准备初始化一个module,为截获httprequest做准备
///
///
public void Init(HttpApplication context)
{
//通过两个事件订阅,加入自己的处理
context.BeginRequest+=new EventHandler(this.Application_BeginReqest);
context.EndRequest+=new EventHandler(this.Application_EndReqest);
}
public void Application_BeginReqest(object sender,EventArgs e)
{
HttpApplication app = sender as HttpApplication;
if(app!=null)
{
HttpContext context = app.Context;
context.Response.Write("this is in module begin request ");
}
}
public void Application_EndReqest(object sender, EventArgs e)
{
HttpApplication httpApp = sender as HttpApplication;
if(httpApp!=null)
{
HttpContext context = httpApp.Context;
context.Response.Write("this is in module end request");
}
}
}
建立hellomodule页面,访问,页面输出如下
我们在建立一个HttpModule跟原来的一样,只不过命名为HttpModule2,添加配置。
IHttpHandler定义如下
public interface IHttpHandler
{
// Methods
void ProcessRequest(HttpContext context);
// Properties
bool IsReusable { get; }
}
下边是我自己实现的HttpHandler
public class MyHandler : IHttpHandler
{
public MyHandler()
{
}
///
/// 根据webconfig中的httpHandler节点的path的配置,当访问该节点时,就会自动生成相应的type实例,调用该handler的
/// ProcessRequest方法
/// 所以,我们可以通过解析路径的方法,让路径对应一个handler中的某个方法,可以使用发射工厂
///
///
public void ProcessRequest(HttpContext context)
{
Uri uri= context.Request.Url;
context.Response.Write("
"+uri.OriginalString);
context.Response.Write("
Hello,this is my http handler
");
}
public bool IsReusable
{
get { return true; }
}
}
然后在浏览器中访问HelloHandler,如下所示
我们也可以将该path对应一个aspx页面,当然这样就像是给这个路径指定了两个HttpHandler。新建一个HelloHandler.aspx页面,添加内容【 this is in page!!!!!!!!!!!】,然后添加web.Config的Handler配置,我们看看效果
看来page对应的Handler没有生效。。。。
HttpHandler和HttpModule小小总结
输出内容如下
查看PageHandlerFactory 代码发现
public virtual IHttpHandler GetHandler(HttpContext context, string requestType, string virtualPath, string path)
{
return this.GetHandlerHelper(context, requestType, VirtualPath.CreateNonRelative(virtualPath), path);
}
private IHttpHandler GetHandlerHelper(HttpContext context, string requestType, VirtualPath virtualPath, string physicalPath)
{
Page page = BuildManager.CreateInstanceFromVirtualPath(virtualPath, typeof(Page), context, true) as Page;
if (page == null)
{
return null;
}
page.TemplateControlVirtualPath = virtualPath;
return page;
}