代码改变世界

Ihttphandler,Ihttpmodule

2011-09-14 17:20  Tracy.  阅读(370)  评论(0编辑  收藏  举报

1.ihttpModule(粘合剂,侦听application事件或用户自定义事件。)

2.ihttpHandler(可以自定义对特定资源进行处理)

2.ihttpHandlerFactory(可以用来进行在自定义ihttpmodule或标准的ihttpModule之间进行选择)

ihttpHandler拦截httpRequest.

ihttpModule是事件侦听器(其作用跟glob.aspx有些相同)

区别:

ihttpHandler 完全负责页面的请求和响应

ihttpModule 负责在其Init事件中申请的HttpApplication事件,执行完还要按HttpApplication其他事件执行

应用程序按照以下顺序执行由 global.asax 文件中定义的模块或用户代码处理的事件:

1.BeginRequest

2.AuthenticateRequest

3.PostAuthenticateRequest

4.AuthorizeRequest

5.PostAuthorizeRequest

6.ResolveRequestCache

7.PostResolveRequestCache

PostResolveRequestCache 事件之后、PostMapRequestHandler 事件之前创建一个事件处理程序(对应于请求 URL 的页)。

8.PostMapRequestHandler

9.AcquireRequestState

10.PostAcquireRequestState

11.PreRequestHandlerExecute

执行事件处理程序。

12.PostRequestHandlerExecute

13.ReleaseRequestState

14.PostReleaseRequestState

PostReleaseRequestState 事件之后,响应筛选器(如果有)将对输出进行筛选。

15.UpdateRequestCache

16.PostUpdateRequestCache

17.EndRequest

默认的一些事件的处理ihttpmodule

<httpModules>

<add name="OutputCache" type="System.Web.Caching.OutputCacheModule" />

<add name="Session" type="System.Web.SessionState.SessionStateModule" />

<add name="WindowsAuthentication" type="System.Web.Security.WindowsAuthenticationModule" />

<add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />

<add name="PassportAuthentication" type="System.Web.Security.PassportAuthenticationModule" />

<add name="RoleManager" type="System.Web.Security.RoleManagerModule" />

<add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" />

<add name="FileAuthorization" type="System.Web.Security.FileAuthorizationModule" />

<add name="AnonymousIdentification" type="System.Web.Security.AnonymousIdentificationModule" />

<add name="Profile" type="System.Web.Profile.ProfileModule" />

</httpModules>

具体使用方法:
主要是继承IHttpModule和IHttpHandler来重写其 中的方法,IhttpModule 中重写其Init方法及各种请示过程事件和Dispose方法

using System.Web;
using System;

namespace CustomerHttpModules
{
/**//// <summary>
/// Class1 的摘要说明。
/// </summary>
public class MyHttpModules:IHttpModule
{
public MyHttpModules()
{

}
IHttpModule 成员#region IHttpModule 成员

void System.Web.IHttpModule.Init(HttpApplication context)
{
context.BeginRequest+=new EventHandler(context_BeginRequest);
context.EndRequest += new EventHandler(this.context_EndRequest);
}
public void context_BeginRequest(object obj,System.EventArgs e)
{
HttpApplication app = (HttpApplication)obj;
app.Response.Write("Hi,testing customer http module request");
}
public void context_EndRequest(object obj,System.EventArgs e)
{
HttpApplication app=(HttpApplication)obj;
app.Response.Write("Hi,end request here");
}
public void Dispose()
{
// TODO: 添加 MyHttpModules.Dispose 实现
}

#endregion
}
}

然后生成dll,新建个asp.net应用,引用此dll,在web.config中加入

<httpModules>
<add name="test" type="CustomerHttpModules.MyHttpModules,CustomerHttpModules"/>
</httpModules>

则当有页面请求时会查找到此dll,然后执行其中过程。

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

以下为重写IHttpHandler中方法的代码:

同样地建个类库

using System;
using System.Web;

namespace CustomerHttpHandler
{
/**//// <summary>
/// Class1 的摘要说明。
/// </summary>
public class TestCustomerHttpHandler:IHttpHandler
{
public TestCustomerHttpHandler()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
IHttpHandler 成员#region IHttpHandler 成员

public void ProcessRequest(HttpContext context)
{
context.Response.Write("hi,I write something instead of what you input");
}

public bool IsReusable
{
get
{
// TODO: 添加 TestCustomerHttpHandler.IsReusable getter 实现
return true;
}
}

#endregion
}
}

所有页面请示的核心处理都是通过IHttpHandler的ProcessRequest方法来完成,因此只要我们重写此方法,则无论页面有任何请求,都会被我们所重写的内容代替掉。嘿嘿

生成dll后再在asp.net应用中的 web.config 中加入

<httpHandlers>
<add verb="*" path="*" type="CustomerHttpModules.MyHttpModules,CustomerHttpModules"/>
</httpHandlers>