HttpModule,ASP.NET中多个HttpModule的处理


using System;
using System.Web;

namespace xumh
{
    
/// <summary>
    
/// 多个HttpModule的应用示例:
    
/// web.config文件中HttpModules里面先Add哪一个他就先执行,按add的顺序分别执行
    
/// 多个HttpModule要过滤的事件会分别处理
    
/// </summary>
    public class multiHttpModule : IHttpModule
    {
        
public void Dispose()
        {
            
throw new NotImplementedException();
        }

        
public void Init(HttpApplication context)
        {
//重新定义ASP.NET的事件处理程序
            context.BeginRequest += new EventHandler(myBeginRequest);
            context.EndRequest 
+= new EventHandler(myEndRequest);
            context.PreRequestHandlerExecute 
+= new EventHandler(myPreRequestHandlerExecute);
            context.PostRequestHandlerExecute 
+= new EventHandler(myPostRequestHandlerExecute);
            context.ReleaseRequestState 
+= new EventHandler(myReleaseRequestState);
            context.AcquireRequestState 
+= new EventHandler(myAcquireRequestState);
            context.AuthenticateRequest 
+= new EventHandler(myAuthenticateRequest);
            context.AuthorizeRequest 
+= new EventHandler(myAuthorizeRequest);
            context.ResolveRequestCache 
+= new EventHandler(myResolveRequestCache);
            context.PreSendRequestHeaders 
+= new EventHandler(myPreSendRequestHeaders);
            context.PreSendRequestContent 
+= new EventHandler(myPreSendRequestContent);
        
        }

        
void myPreSendRequestContent(object sender, EventArgs e)
        {
            HttpApplication app 
= (HttpApplication)sender;
            HttpContext context 
= app.Context;
            context.Response.Write(
"myPreSendRequestContent<br/>");
        }

        
void myPreSendRequestHeaders(object sender, EventArgs e)
        {
            HttpApplication app 
= (HttpApplication)sender;
            HttpContext context 
= app.Context;
            context.Response.Write(
"myPreSendRequestHeaders<br/>");
        }

        
void myResolveRequestCache(object sender, EventArgs e)
        {
            HttpApplication app 
= (HttpApplication)sender;
            HttpContext context 
= app.Context;
            context.Response.Write(
"myResolveRequestCache<br/>");
        }

        
void myAuthorizeRequest(object sender, EventArgs e)
        {
            HttpApplication app 
= (HttpApplication)sender;
            HttpContext context 
= app.Context;
            context.Response.Write(
"BeginRequest<br/>");
        }

        
void myAuthenticateRequest(object sender, EventArgs e)
        {
            HttpApplication app 
= (HttpApplication)sender;
            HttpContext context 
= app.Context;
            context.Response.Write(
"BeginRequest<br/>");
        }

        
void myAcquireRequestState(object sender, EventArgs e)
        {
            HttpApplication app 
= (HttpApplication)sender;
            HttpContext context 
= app.Context;
            context.Response.Write(
"myAcquireRequestState<br/>");
        }

        
void myReleaseRequestState(object sender, EventArgs e)
        {
            HttpApplication app 
= (HttpApplication)sender;
            HttpContext context 
= app.Context;
            context.Response.Write(
"myReleaseRequestState<br/>");
        }

        
void myPostRequestHandlerExecute(object sender, EventArgs e)
        {
            HttpApplication app 
= (HttpApplication)sender;
            HttpContext context 
= app.Context;
            context.Response.Write(
"myPostRequestHandlerExecute<br/>");
        }

        
void myPreRequestHandlerExecute(object sender, EventArgs e)
        {
            HttpApplication app 
= (HttpApplication)sender;
            HttpContext context 
= app.Context;
            context.Response.Write(
"myPreRequestHandlerExecute<br/>");
        }

        
void myEndRequest(object sender, EventArgs e)
        {
            HttpApplication app 
= (HttpApplication)sender;
            HttpContext context 
= app.Context;
            context.Response.Write(
"myEndRequest<br/>");
        }

        
void myBeginRequest(object sender, EventArgs e)
        {
            HttpApplication app 
= (HttpApplication)sender;
            HttpContext context 
= app.Context;
            context.Response.Write(
"multiHttpModules:myBeginRequest<br/>");
        }

    }
}
/*--===------------------------------------------===---
输出结果:
 testHttpModule:myBeginRequest
multiHttpModules:myBeginRequest
BeginRequest
BeginRequest
myResolveRequestCache
myAcquireRequestState
myPreRequestHandlerExecute

 
myPostRequestHandlerExecute
myReleaseRequestState
myEndRequest
myPreSendRequestHeaders
--===------------------------------------------===---
*/
原文地址:https://www.cnblogs.com/flaaash/p/996989.html