ASP.NET IHttpModule

简介: IHttpModule接口的定义:向实现类提供模块初始化和处置事件。它包含2个方法:Dispose()和Init(); 自定义IHttpModule接口实现一个自定义的IHttpModule接口需要2个步骤: 1)实现一个继承了IHttpModule接口的类 2)在Web.

IHttpModule接口的定义:向实现类提供模块初始化和处置事件。它包含2个方法:Dispose()和Init();

自定义IHttpModule接口
实现一个自定义的IHttpModule接口需要2个步骤:

1)实现一个继承了IHttpModule接口的类

2)在Web.config文件中注册这个自定义HttpModule

view source

print?01 public class CustomerModule:IHttpModule

02    {

03        #region IHttpModule 成员

04        public void Dispose()

05        {

06              

07        }

08        public void Init(HttpApplication context)

09        {

10            context.BeginRequest += new EventHandler(context_BeginRequest);

11            context.EndRequest += new EventHandler(context_EndRequest);

12        }

13        void context_BeginRequest(object sender, EventArgs e)

14        {

15            HttpApplication application = (HttpApplication)sender;

16            application.Context.Response.Write("自定义ModuleRequest开始");

17        }

18        void context_EndRequest(object sender, EventArgs e)

19        {

20            HttpApplication application = (HttpApplication)sender;

21            application.Context.Response.Write("自定义ModuleRequest结束");

22        }

23        #endregion

24    }

 

web.config

view source

print?1 <httpModules>

2   <add name="customerModule" type="WebApplication3.CustomerModule, WebApplication3"/>

3 </httpModules>

ASP.NET 内置的HttpModule
view source

print?1 <FONT color=#7b7d62>在C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\web.config下有很多ASP.NET内置的HttpMoudle默认是加载的如果你不想加载其中的一些可以Remove掉这样可以提高一些性能如:</FONT>

view source

print?1 <remove name="WindowsAuthentication"/>

view source

print?1 内置的如下:

view source

print?01 <httpModules>

02     <add name="OutputCache" type="System.Web.Caching.OutputCacheModule"/>

03     <add name="Session" type="System.Web.SessionState.SessionStateModule"/>

04     <add name="WindowsAuthentication" type="System.Web.Security.WindowsAuthenticationModule"/>

05     <add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule"/>

06     <add name="PassportAuthentication" type="System.Web.Security.PassportAuthenticationModule"/>

07     <add name="RoleManager" type="System.Web.Security.RoleManagerModule"/>

08     <add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule"/>

09     <add name="FileAuthorization" type="System.Web.Security.FileAuthorizationModule"/>

10     <add name="AnonymousIdentification" type="System.Web.Security.AnonymousIdentificationModule"/>

11     <add name="Profile" type="System.Web.Profile.ProfileModule"/>

12     <add name="ErrorHandlerModule" type="System.Web.Mobile.ErrorHandlerModule, System.Web.Mobile, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>

13     <add name="ServiceModel" type="System.ServiceModel.Activation.HttpModule, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>

14 </httpModules>

Global.asax与HttpModule
view source

print?1 <FONT color=#7b7d62>在ASP.NET中Global.asax是一个全局文件,他可以注册应用程序和Session事件,还可以注册HttpModule中暴露的事件(包括内置的HttpModule和自定义的HttpModule),有2点注意事项:</FONT>

view source

print?1 <FONT color=#7b7d62>1)每当一个Http Request到达时,应用程序事件都要出发遍,但是Application_Start和Application_End仅在第一个资源文件被访问时触发。</FONT>

view source

print?1 <FONT color=#7b7d62>2)HttpModule无法注册和响应Session 事件,Session事件只能在Global.asax中注册</FONT>

view source

print?01 public class CustomerModule:IHttpModule

02 {

03     //自定义暴露的事件

04     public event EventHandler ExposedEvent;

05     

06     #region IHttpModule 成员

07     public void Dispose()

08     {

09           

10     }

11     public void Init(HttpApplication context)

12     {

13         context.BeginRequest += new EventHandler(context_BeginRequest);

14         context.EndRequest += new EventHandler(context_EndRequest);

15     }

16     void context_BeginRequest(object sender, EventArgs e)

17     {

18         HttpApplication application = (HttpApplication)sender;

19         application.Context.Response.Write("自定义ModuleRequest开始");

20     }

21     void context_EndRequest(object sender, EventArgs e)

22     {

23         HttpApplication application = (HttpApplication)sender;

24         application.Context.Response.Write("自定义ModuleRequest结束");

25         //触发事件

26         OnExposedEvent(new EventArgs());

27     }

28     #endregion

29       

30     protected void OnExposedEvent( EventArgs e)

31     {

32         if (ExposedEvent!=null)

33         {

34             ExposedEvent(this, e);

35         }

36     }

37 }

 

在Global.asax中添加这样的代码

view source

print?1 protected void customerModule_ExposedEvent(object sender, EventArgs e)

2 {

3     Response.Write("来自Global.asax");

4 }

 

其中Global.asax中事件名的定义是:模块名_事件名。其中模块名是你在Web.config中注册的模块名称

相关文章
|
Web App开发 .NET 索引
asp.net 利用IHttpModule和IRequiresSessionState控制入口登录开发错误解决办法
1、VS项目中添加FilterModule.cs来控制从登陆页面进入系统,该类代码如下: using System; using System.
949 0
|
1月前
|
开发框架 前端开发 JavaScript
ASP.NET MVC 教程
ASP.NET 是一个使用 HTML、CSS、JavaScript 和服务器脚本创建网页和网站的开发框架。
34 7
|
1月前
|
存储 开发框架 前端开发
ASP.NET MVC 迅速集成 SignalR
ASP.NET MVC 迅速集成 SignalR
47 0
|
2月前
|
开发框架 前端开发 .NET
ASP.NET MVC WebApi 接口返回 JOSN 日期格式化 date format
ASP.NET MVC WebApi 接口返回 JOSN 日期格式化 date format
40 0
|
2月前
|
开发框架 前端开发 安全
ASP.NET MVC 如何使用 Form Authentication?
ASP.NET MVC 如何使用 Form Authentication?
|
2月前
|
开发框架 .NET
Asp.Net Core 使用X.PagedList.Mvc.Core分页 & 搜索
Asp.Net Core 使用X.PagedList.Mvc.Core分页 & 搜索
104 0
|
5月前
|
开发框架 前端开发 .NET
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
169 0
|
5月前
|
开发框架 前端开发 JavaScript
JavaScript云LIS系统源码ASP.NET CORE 3.1 MVC + SQLserver + Redis医院实验室信息系统源码 医院云LIS系统源码
实验室信息系统(Laboratory Information System,缩写LIS)是一类用来处理实验室过程信息的软件,云LIS系统围绕临床,云LIS系统将与云HIS系统建立起高度的业务整合,以体现“以病人为中心”的设计理念,优化就诊流程,方便患者就医。
73 0
|
5月前
|
开发框架 前端开发 .NET
C# .NET面试系列六:ASP.NET MVC
<h2>ASP.NET MVC #### 1. MVC 中的 TempData\ViewBag\ViewData 区别? 在ASP.NET MVC中,TempData、ViewBag 和 ViewData 都是用于在控制器和视图之间传递数据的机制,但它们有一些区别。 <b>TempData:</b> 1、生命周期 ```c# TempData 的生命周期是短暂的,数据只在当前请求和下一次请求之间有效。一旦数据被读取,它就会被标记为已读,下一次请求时就会被清除。 ``` 2、用途 ```c# 主要用于在两个动作之间传递数据,例如在一个动作中设置 TempData,然后在重定向到另
300 5
|
5月前
|
开发框架 前端开发 .NET
进入ASP .net mvc的世界
进入ASP .net mvc的世界