HttpModule

HttpModules allow we to extend the existing ASP.NET pipeline.  The most common use of HTTPModules is pre and/ore postprecessing of requests.

From the article we can understand the principle, there is a detail flow when request is happen

HttpRequest --> inetinfor.exe --> AspNet_ISAPI.dll --> Http pipeline --> AspNet_WP.exe
--> HttpRuntime --> HttpApplication Factory --> HttpApplication --> HttpModule -->
HttpHandler Factory --> HttpHandler --> HttpHandler.ProcessRequest()

I wrote a test class inherit from interface IHttpModule
     public   class  MyHttpModule : IHttpModule 
    
{
        
public MyHttpModule()
        
{
            
//
            
// TODO: Add constructor logic here
            
//
        }


        
public void Init(HttpApplication application)
        
{
            application.BeginRequest 
+= (new EventHandler(this.Application_BeginRequest));
            application.EndRequest 
+= (new EventHandler(this.Application_EndRequest));
            application.PreRequestHandlerExecute 
+=(new EventHandler(this.Application_PreRequestHandlerExecute));
            application.PostRequestHandlerExecute 
+=(new EventHandler(this.Application_PostRequestHandlerExecute));
            application.ReleaseRequestState 
+=(new EventHandler(this.Application_ReleaseRequestState));
            application.AcquireRequestState 
+=(new EventHandler(this.Application_AcquireRequestState));
            application.AuthenticateRequest 
+=(new EventHandler(this.Application_AuthenticateRequest));
            application.AuthorizeRequest 
+=(new EventHandler(this.Application_AuthorizeRequest));
            application.ResolveRequestCache 
+=(new EventHandler(this.Application_ResolveRequestCache));
            application.PreSendRequestHeaders 
+=(new EventHandler(this.Application_PreSendRequestHeaders));
            application.PreSendRequestContent 
+=(new EventHandler(this.Application_PreSendRequestContent));
        }

        
        
private void Application_PreRequestHandlerExecute(Object source, EventArgs e)
        
{
            HttpApplication application 
= (HttpApplication)source;
            HttpContext context 
= application.Context;

            context.Response.Write(
"Application_PreRequestHandlerExecute<br>");
        }


        
private void Application_BeginRequest(Object source, EventArgs e) 
        
{
            HttpApplication application 
= (HttpApplication)source;
            HttpContext context 
= application.Context;

            context.Response.Write(
"Application_BeginRequest<br>");
        }


        
private void Application_EndRequest(Object source, EventArgs e) 
        
{
            HttpApplication application 
= (HttpApplication)source;
            HttpContext context 
= application.Context;

            context.Response.Write(
"Application_EndRequest<br>");

        }
 

        
private void Application_PostRequestHandlerExecute(Object source,EventArgs e)
        
{
            HttpApplication application 
= (HttpApplication)source;
            HttpContext context 
= application.Context;

            context.Response.Write(
"Application_PostRequestHandlerExecute<br>");

        }


        
private void Application_ReleaseRequestState(Object source, EventArgs e)
        
{
            HttpApplication application 
= (HttpApplication)source;
            HttpContext context 
= application.Context;

            context.Response.Write(
"Application_ReleaseRequestState<br>");

        }


        
private void Application_UpdateRequestCache(Object source, EventArgs e)
        
{
            HttpApplication application 
= (HttpApplication)source;
            HttpContext context 
= application.Context;

            context.Response.Write(
"Application_UpdateRequestCache<br>");

        }


        
private void Application_AuthenticateRequest(Object source, EventArgs e)
        
{
            HttpApplication application 
= (HttpApplication)source;
            HttpContext context 
= application.Context;

            context.Response.Write(
"Application_AuthenticateRequest<br>");

        }


        
private void Application_AuthorizeRequest(Object source, EventArgs e)
        
{
            HttpApplication application 
= (HttpApplication)source;
            HttpContext context 
= application.Context;

            context.Response.Write(
"Application_AuthorizeRequest<br>");

        }


        
private void Application_ResolveRequestCache(Object source, EventArgs e)
        
{
            HttpApplication application 
= (HttpApplication)source;
            HttpContext context 
= application.Context;

            context.Response.Write(
"Application_ResolveRequestCache<br>");

        }


        
private void Application_AcquireRequestState(Object source, EventArgs e)
        
{
            HttpApplication application 
= (HttpApplication)source;
            HttpContext context 
= application.Context;

            context.Response.Write(
"Application_AcquireRequestState<br>");

        }


        
private void Application_PreSendRequestHeaders(Object source, EventArgs e)
        
{
            HttpApplication application 
= (HttpApplication)source;
            HttpContext context 
= application.Context;

            context.Response.Write(
"Application_PreSendRequestHeaders<br>");

        }


        
private void Application_PreSendRequestContent(Object source, EventArgs e)
        
{
            HttpApplication application 
= (HttpApplication)source;
            HttpContext context 
= application.Context;

            context.Response.Write(
"Application_PreSendRequestContent<br>");

        }


        
public void Dispose(){}

    }

I am surprised to find that when the transcation in HttpHandler is finished the request will return to HttpModule,below is the lifcycle of request in HttpModule.

Http Request
        |
HttpModule
        |
HttpModule.BeginRequest()
        |
HttpModule.AuthenticateRequest()
        |
HttpModule.AuthorizeRequest()
        |
HttpModule.ResolveRequestCache()
        |
Create HttpHandler reference point
        |
HttpHandler is begin
        |
HttpModule.AcquireRequestState()
        |
HttpModule.PreRequestHandlerExecute()
        |
Enter into HttpHandler to treat with the HttpRequest
        |
HttpHandler.ProcessRequest()
        |
Return to HttpModule( HttpHandler is over
        |
HttpModule.PostRequestHandlerExecute()
        |
HttpModule.ReleaseRequestState()
        |
HttpModule.UpdateRequestCache()
        |
HttpModule.EndRequest()
        |
HttpModule.PreSendRequestHeaders()
        |
HttpModule.PreSendRequestContent()
        |
return the data to client
        |
The whole transaction of  Http Request is over
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值