CSDN博客

img tomyao

HttpModule

发表于2004/9/16 13:33:00  808人阅读

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

相关博文

我的热门文章

img
取 消
img