






















以前没有接触过这块,现在趁着有时间便看了看,我是从了解HttpModule和HttpHandler开始的,当我们请求.aspx页面时,服务器的IIS服务会把相应的处理交给ASPNET_ISAPI.DLL,而aspnet_isapi.dll会通过一个"管道"传给asp.net相应的进程处理,进入HttpRuntime,这时就有一个HttpApplication Factory的容器接收http请求,并依次进行:HttpModule-HttpApplication Factory-HttpHandler,处理完毕后便会返回,HttpModule 会贯穿始终,只不过会在中间一小块把控制权交给HttpHandler。要应用HttpModule和HttpHandler则必须从IHttpModule和IHttpHandler接口进行实现,http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/cpguide/html/cpconHttpModules.asp这里便是msdn对HttpModule的简要说明:
HttpModule 是实现 IHttpModule 接口和处理事件的程序集。ASP.NET 包含一组可由应用程序使用的 HttpModule 模块。例如,ASP.NET 提供了 SessionStateModule 来向应用程序提供会话状态服务。可以创建自定义 HttpModule 以响应 ASP.NET 事件或用户事件。
编写 HttpModule 的一般过程为:
在Init里我们可以声明一些委托:下面是我的例子:
public void Init(HttpApplication application)
{
//在asp.net响应请求时作为Http执行管线连中的第一个事件发生
application.BeginRequest+=new EventHandler(application_BeginRequest);
application.EndRequest+=new EventHandler(application_EndRequest);//最后一个
application.PreRequestHandlerExecute+=new EventHandler(application_PreRequestHandlerExecute);
application.PreSendRequestContent+=new EventHandler(application_PreSendRequestContent);
application.AcquireRequestState+=new EventHandler(application_AcquireRequestState);
application.AuthenticateRequest+=new EventHandler(application_AuthenticateRequest);
application.AuthorizeRequest+=new EventHandler(application_AuthorizeRequest);
application.PostRequestHandlerExecute+=new EventHandler(application_PostRequestHandlerExecute);
application.PreSendRequestHeaders+=new EventHandler(application_PreSendRequestHeaders);
application.ReleaseRequestState+=new EventHandler(application_ReleaseRequestState);
application.ResolveRequestCache+=new EventHandler(application_ResolveRequestCache);
application.UpdateRequestCache+=new EventHandler(application_UpdateRequestCache);
}
在这里我把HttpApplication的全部事件都声明了一遍,然后在每个方法的Response.Write写到浏览器一些内容,下面便是执行顺序:
This is BeginRequest
This is AuthenticateRequest
This is AuthorizeRequest
This is ResolveRequestCache
This is AcquireRequestState
This is PreRequestHandlerExecute
This is PostRequestHandlerExecute
This is ReleaseRequestState
This is UpdateRequestCache
This is EndRequest
This is PreSendRequestHeaders
This is PreSendRequestContent
大家可以看到,BeginRequest是第一个,中间的Hello World是发生HttpHandler的地方,大家自己写的时候可以单步跟踪一下就会明白了。说了这么多还没提到Url重写,我们的Url重写处理方法就放在这个过程中,因为这是http请求的必经之路。http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx?mfr=true这里有篇介绍Url重写的文章写的比较经典,还有源码下载。作者提供了两个方法重写Url,两个方法即两个不同的地方,一个在HttpModule中一个在HttpHandler中,不过并不是实现的IHttpHandler接口而是IHttpHandlerFactory接口,具体处理方法可下载源码并进行单步跟踪。如果看明白了,那么恭喜你,跟我一样,对Url重写入门了,呵呵。。。。。
ps:建议现看看《ASP.NET Framework深度历险》这个55页电子书,这样在看Scott Mitchell的那篇Url的文章会好懂些
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。