HttpModule教程

HttpModule教程

在asp.net上下载的communityStarterKit,研究了很长时间都不知道它的页面是如何实现跳转的,而且它的超链接我根本找不到。最后进行断点跟踪,终于找到CommunitiesModule类,发现了其继承自IHttpModule,接着在项目中搜索CommunitiesModule,终于发现了webconfig.xml中有这么一段
<system.web>  
        <compilation debug="true" />
        <customErrors mode="Off"/>
        <httpModules>
            <add name="CommunitiesModule" type="ASPNET.StarterKit.Communities.CommunitiesModule,ASPNET.StarterKit.Communities" />
        </httpModules>
然后就是看帮助,上网搜,终于知道了。。。。

ASP.Net中自定义Http处理及应用之HttpModule篇
作者:孙亚民    
HttpHandler实现了类似于ISAPI Extention的功能,他处理请求(Request)的信息和发送响应(Response)。HttpHandler功能的实现通过实现IHttpHandler接口来达到。而HttpModule实现了类似于ISAPI Filter的功能。

HttpModule的实现


HttpModules实现了类似于ISAPI Filter的功能,在开发上,通常需要经过以下步骤:

1.编写一个类,实现IhttpModule接口

2.实现Init 方法,并且注册需要的方法

3.实现注册的方法

4.实现Dispose方法,如果需要手工为类做一些清除工作,可以添加Dispose方法的实现,但这不是必需的,通常可以不为Dispose方法添加任何代码。

5.在Web.config文件中,注册您编写的类

下面是一个HttpModules的示例,在这个示例中,只是简单的注册了HttpApplication 的BeginRequest 和 EndRequest事件,并且通过这些事件的实现方法,将相关的信息打印出来。

例1:
            using System;
            using System.Web;
            namespace MyModule
            {
            public class MyModule : IHttpModule
            {
            public void Init(HttpApplication application)
            {
            application.BeginRequest += (new
            EventHandler(this.Application_BeginRequest));
            application.EndRequest += (new
            EventHandler(this.Application_EndRequest));
            }
            private void Application_BeginRequest(Object source, EventArgs e)
            {
            HttpApplication Application = (HttpApplication)source;
            HttpResponse Response=Application.Context.Response;
            Response.Write("<h1>Beginning of Request</h1><hr>");
            }
            private void Application_EndRequest(Object source, EventArgs e)
            {
            HttpApplication application = (HttpApplication)source;
            HttpResponse Response=Application.Context.Response;
            Response.Write("<h1>End of Request</h1><hr>");
            }
            public void Dispose()
            {
            }
            }
            }


程序的开始引用了如下名称空间:

using System;
            using System.Web;


因为HttpApplication、HttpContext、HttpResponse等类在System.Web中定义,因此,System.Web名称空间是必须引用的。

MyModule类实现了IhttpModule接口。在Init方法中,指明了实现BeginRequest 和EndRequest 事件的方法。在这两个方法中,只是简单的分别打印了一些信息。

下面,在Web.config文件中注册这个类,就可以使用这个HttpModule了,注册的方法如下:

<configuration>
            <system.web>
            <httpModules>
            <add name=" MyModule " type=" MyModule, MyModule" />
            </httpModules>
            </system.web>
            </configuration>


现在来看一下效果。编写一个Aspx页面test.aspx,内容如下:

<%
            Response.Write("<h1>This is the Page</h1><hr>");
            %>


运行以后的界面如图所示:



深入研究HttpModule


HttpModule通过对HttpApplication对象的一系列事件的处理来对HTTP处理管道施加影响,这些事件在HttpModule的Init方法中进行注册,包括:

BeginRequest
            AuthenticateRequest
            AuthorizeRequest
            ResolveRequestCache
            AcquireRequestState
            PreRequestHandlerExecute
            PostRequestHandlerExecute
            ReleaseRequestState
            UpdateRequestCache
            EndRequest


其中部分事件同Global.asax中的事件相对应,对应关系如下:

HttpModule Global.asax
BeginRequest Application_BeginRequest
AuthenticateRequest Application_AuthenticateRequest
EndRequest Application_EndRequest


在例1中,处理了BeginRequest和EndRequest事件,其他事件的处理方式基本上类似。

同HttpHandler对应来看,这些事件,有些在HttpHandler之前发生,有些在HttpHandler处理完后发生。了解事件发生的顺序非常重要,因为,服务器端的对象在不同的时间段有着不同的表现。例子之一是Session的使用。不是所有的事件中都能对Session进行处理,而只能在有限的几个事件中进行处理。详细的过程可以参考下面的HTTP Request处理生命周期图。



使用HttpModule实现权限系统


我们在开发应用系统的时候,应用系统的权限控制是非常重要的一个部分。在ASP中,要实现权限的控制是比较麻烦的事情,因为我们必须在每个需要控制权限的ASP页面中添加权限控制代码,从而控制客户对页面的访问。这样带来的问题,除了编写大量重复代?/span>
posted @ 2008-06-24 17:22  Be Myself  阅读(288)  评论(0编辑  收藏  举报