IHttpModule 与 IHttpHandler 浅释(一)

本文介绍了.NET中IHttpModule与IHttpHandler的基本概念及应用实例,重点讲解了IHttpModule接口的作用,包括预处理、URL重写、访问日志等功能,并通过一个登录验证的例子展示了如何使用IHttpModule。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  最近几天开始学习.NET运行的相关知识,其中涉及到 IHttpModule 与 IHttpHandler 两个部份,<br/>

  也是我们每一个。NET程序中负责处理我们相应代码的两大部份!<br/>

  在这里说说我对这两个部份的入门级认识,有什么错误之处希望大家可以指正指正!<br/>

  首先是IHttpModule 接口,从MSDN中获得的代码如下:

 
 

        
ContractedBlock.gif ExpandedBlockStart.gif 代码

          
[AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]  
public interface IHttpModule{   
// 释放资源   
   void Dispose();
  
void Init(HttpApplication context); // 初始化模块,并使其为处理请求做好准备   
}

 

利用IHttpModule我们可以一些需要的功能:
例如:1:预处理 (验证、修改、过虑..)2:URL重写 3:访问日志 4:流量统计等等
第一个方法我相信大家也大概可以知道有什么作用了吧?
现在我们看下第二个方法:
在此方法中初始化了我们很多平时需要用到的数据:SERVER、REQUEST、REPONSE、Application、SESSION等等<br/>
此方法里也按照以下执行顺序由GLOBAL.ASPX文件中定义的模块与用户代码处理事件(.NET 2.0版本)

BeginRequest

AuthenticateRequest

PostAuthenticateRequest

AuthorizeRequest

PostAuthorizeRequest

ResolveRequestCache

PostResolveRequestCache

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

PostMapRequestHandler

AcquireRequestState

PostAcquireRequestState

PreRequestHandlerExecute

执行事件处理程序。

PostRequestHandlerExecute

ReleaseRequestState

PostReleaseRequestState

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

UpdateRequestCache

PostUpdateRequestCache

EndRequest

以下是一个登录验证的例子,希望有助于大家去理解:

 

ContractedBlock.gif ExpandedBlockStart.gif 代码

      
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;


namespace TestModule
{
public class LoginFilter : IHttpModule
{
s


public void Init(HttpApplication context)
{
// 以下四个事件中可以访问SESSION。
// context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
// context.AcquireRequestState += new EventHandler(context_AcquireRequestState);
// context.PostAcquireRequestState += new EventHandler(context_PostAcquireRequestState);
context.PostRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
}

public void context_PreRequestHandlerExecute( object sender, EventArgs e)
{
HttpApplication hApp
= (HttpApplication)sender;
string URL = hApp.Context.Request.Url.ToString();
int la = URL.ToLower().ToString().IndexOf( " login.aspx " );
if (la == - 1 )
{
if (hApp.Context.Session[ " userId " ] == null )
{
hApp.Context.Response.Write(
" <script language='javascript'>alert('您未登陆或登陆已超时,请重新登陆!');</script> " );
hApp.Context.Response.Redirect(
" ../Login.aspx?source= " + path);
}
}
}


}
}

 

1:在需要应用的项目中增加TestModule.dll引用;

2:在项目中的Web.Config添加以下代码:

 


     
< httpModules >
< add name = " LoginFilter " type = "Test Module.LoginFilter,TestModule " />
</ httpModules >

 

补充:

1:在此例子中我们实现的是一个简单的登陆验证,在。NET2.0中的十六个事件中,其中只有PreRequestHandlerExecute、AcquireRequestState、PostAcquireRequestState、PostRequestHandlerExecute这4个可以访问SESSION.

2:在Web.config中,httpModules由名字空间、类名称和程序集名称组成

posted on 2010-09-19 15:38 闷蛋 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/playonto/archive/2010/09/19/playonto.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值