HttpModule

原创 2004年09月16日 13:33:00
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
None.gif    public class MyHttpModule : IHttpModule 
ExpandedBlockStart.gif    
{
InBlock.gif        
public MyHttpModule()
ExpandedSubBlockStart.gif        
{
InBlock.gif            
//
InBlock.gif            
// TODO: Add constructor logic here
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

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

InBlock.gif        
InBlock.gif        
private void Application_PreRequestHandlerExecute(Object source, EventArgs e)
ExpandedSubBlockStart.gif        
{
InBlock.gif            HttpApplication application 
= (HttpApplication)source;
InBlock.gif            HttpContext context 
= application.Context;
InBlock.gif
InBlock.gif            context.Response.Write(
"Application_PreRequestHandlerExecute<br>");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void Application_BeginRequest(Object source, EventArgs e) 
ExpandedSubBlockStart.gif        
{
InBlock.gif            HttpApplication application 
= (HttpApplication)source;
InBlock.gif            HttpContext context 
= application.Context;
InBlock.gif
InBlock.gif            context.Response.Write(
"Application_BeginRequest<br>");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void Application_EndRequest(Object source, EventArgs e) 
ExpandedSubBlockStart.gif        
{
InBlock.gif            HttpApplication application 
= (HttpApplication)source;
InBlock.gif            HttpContext context 
= application.Context;
InBlock.gif
InBlock.gif            context.Response.Write(
"Application_EndRequest<br>");
InBlock.gif
ExpandedSubBlockEnd.gif        }
 
InBlock.gif
InBlock.gif        
private void Application_PostRequestHandlerExecute(Object source,EventArgs e)
ExpandedSubBlockStart.gif        
{
InBlock.gif            HttpApplication application 
= (HttpApplication)source;
InBlock.gif            HttpContext context 
= application.Context;
InBlock.gif
InBlock.gif            context.Response.Write(
"Application_PostRequestHandlerExecute<br>");
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void Application_ReleaseRequestState(Object source, EventArgs e)
ExpandedSubBlockStart.gif        
{
InBlock.gif            HttpApplication application 
= (HttpApplication)source;
InBlock.gif            HttpContext context 
= application.Context;
InBlock.gif
InBlock.gif            context.Response.Write(
"Application_ReleaseRequestState<br>");
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void Application_UpdateRequestCache(Object source, EventArgs e)
ExpandedSubBlockStart.gif        
{
InBlock.gif            HttpApplication application 
= (HttpApplication)source;
InBlock.gif            HttpContext context 
= application.Context;
InBlock.gif
InBlock.gif            context.Response.Write(
"Application_UpdateRequestCache<br>");
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void Application_AuthenticateRequest(Object source, EventArgs e)
ExpandedSubBlockStart.gif        
{
InBlock.gif            HttpApplication application 
= (HttpApplication)source;
InBlock.gif            HttpContext context 
= application.Context;
InBlock.gif
InBlock.gif            context.Response.Write(
"Application_AuthenticateRequest<br>");
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void Application_AuthorizeRequest(Object source, EventArgs e)
ExpandedSubBlockStart.gif        
{
InBlock.gif            HttpApplication application 
= (HttpApplication)source;
InBlock.gif            HttpContext context 
= application.Context;
InBlock.gif
InBlock.gif            context.Response.Write(
"Application_AuthorizeRequest<br>");
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void Application_ResolveRequestCache(Object source, EventArgs e)
ExpandedSubBlockStart.gif        
{
InBlock.gif            HttpApplication application 
= (HttpApplication)source;
InBlock.gif            HttpContext context 
= application.Context;
InBlock.gif
InBlock.gif            context.Response.Write(
"Application_ResolveRequestCache<br>");
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void Application_AcquireRequestState(Object source, EventArgs e)
ExpandedSubBlockStart.gif        
{
InBlock.gif            HttpApplication application 
= (HttpApplication)source;
InBlock.gif            HttpContext context 
= application.Context;
InBlock.gif
InBlock.gif            context.Response.Write(
"Application_AcquireRequestState<br>");
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void Application_PreSendRequestHeaders(Object source, EventArgs e)
ExpandedSubBlockStart.gif        
{
InBlock.gif            HttpApplication application 
= (HttpApplication)source;
InBlock.gif            HttpContext context 
= application.Context;
InBlock.gif
InBlock.gif            context.Response.Write(
"Application_PreSendRequestHeaders<br>");
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void Application_PreSendRequestContent(Object source, EventArgs e)
ExpandedSubBlockStart.gif        
{
InBlock.gif            HttpApplication application 
= (HttpApplication)source;
InBlock.gif            HttpContext context 
= application.Context;
InBlock.gif
InBlock.gif            context.Response.Write(
"Application_PreSendRequestContent<br>");
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gif        
public void Dispose(){}
InBlock.gif
ExpandedBlockEnd.gif    }

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

HttpApplication处理对象与HttpModule处理模块

一、HttpApplication对象简述   在HttpRuntime创建了HttpContext对象之后,HttpRuntime将随后创建一个用于处理请求的对象,这个对象的类型为HttpAp...
  • letmefish
  • letmefish
  • 2016年08月10日 17:09
  • 898

关于HttpModule中像图片,*.css,*.js等资源文件也被请求问题的解决

   这段时间学习HttpModule,用HttpMoudule写了一个判断权限登录的模块,网上很多,这里不介绍了。我写的代码如下using System;using System.Data;usin...
  • Joetao
  • Joetao
  • 2010年01月23日 21:16
  • 4809

使用HttpModule实现基于角色的身份验证

       最近使用HttpModule实现了一个基于角色的身份验证。我将我的代码贴出,欢迎大家批评指正。程序是以用asp.net 2.0实现的。关于HttpModule的介绍网上有很多的资料,我这...
  • FlyingLimy
  • FlyingLimy
  • 2007年02月26日 10:33
  • 2464

Asp.Net 中使用HttpModule 做Session验证

session的检查可以考虑用一个http module挂在http pipeline上 过程如下: 1. 在Web.Config 配置: ...
  • csharp25
  • csharp25
  • 2014年03月11日 18:07
  • 1591

httpmodule使用

 一个HTTP请求在HttpModule容器的传递过程中,会在某一时刻(ResolveRequestCache事件)将这个HTTP请求传递给HttpHandler容器。在这个事件之后,HttpModu...
  • RainyLin
  • RainyLin
  • 2007年11月08日 23:01
  • 1057

Asp.Net中HttpModule过滤器的简单使用

1.新建一个类库工程    .引用System.Web    .创建一个类,继承接口IHttpModule using System; using System.Collections.Gener...
  • limlimlim
  • limlimlim
  • 2013年08月14日 16:08
  • 7735

IIS7.0 HttpModule 不可用

今天在一个测试项目里写了一个IHttpModule的实现类, 在web.config里配置了:                VS2008运行调试,没有问题,但部署到IIS就不起...
  • ranbolwb
  • ranbolwb
  • 2012年02月21日 17:36
  • 1627

自定义HttpModule实现某些功能的例子

在执行用户请求的时候可能会有一些特殊的要求例如验证用户是否登录,URL重写等。这些问题需要在执行常规代码之前执行,这里就用到了自定义HttpModules。具体的使用方法如下:        自定义一...
  • zy0421911
  • zy0421911
  • 2016年04月18日 10:24
  • 296

ASP.NET-自定义HttpModule与HttpHandler介绍

摘要:本文介绍ASP.NET中自定义HttpModule与HttpHandler,并提供详细的示例代码供参考。     在之前的ASP.NET是如何在IIS下工作的这篇文章中介绍...
  • angel20082008
  • angel20082008
  • 2015年09月12日 12:05
  • 1357

httpmodule通过httpfilter获取返回的网页内容

自定义httpmodulejfdmpublic virtual void Init(HttpApplication app) { // WARNING! Th...
  • cyb331
  • cyb331
  • 2014年07月17日 19:15
  • 984
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:HttpModule
举报原因:
原因补充:

(最多只允许输入30个字)