最近开始关心一些asp.net中比较深入的东西。
今天就先看看HttpModule。
Msdn中有关于HttpModule的介绍,其中有一个自定义 HttpModule 示例,我在IDE里面运行了一下这个小例子,下面简单谈谈我如何测试的:
1.新建WebApplication1 项目
2.添加如下类:HelloWorldModule
自定义HttpModule需要实现接口:IHttpModule,这个接口包括两个方法Init 和 Dispose,其中Init初始化模块,并使其为处理请求做好准备;Dispose处置由实现 IHttpModule 的模块使用的资源(内存除外)。下面的自定义模块只是在任何 HTTP 请求的开头返回一个 Web 页消息和并在处理完该请求后返回另一个 Web 页消息。
using
System;
using System.Web;
using System.Collections;
namespace WebApplication1
{
public class HelloWorldModule : IHttpModule
{
public String ModuleName
{
get { return "HelloWorldModule"; }
}
// In the Init function, register for HttpApplication
// events by adding your handlers.
public void Init(HttpApplication application)
{
application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
application.EndRequest += (new EventHandler(this.Application_EndRequest));
}
// Your BeginRequest event handler.
private void Application_BeginRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
context.Response.Write("<h1><font color=red>HelloWorldModule: Beginning of Request</font></h1><hr>");
}
// Your EndRequest event handler.
private void Application_EndRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
context.Response.Write("<hr><h1><font color=red>HelloWorldModule: End of Request</font></h1>");
}
public void Dispose()
{
}
}
}
using System.Web;
using System.Collections;
namespace WebApplication1
{
public class HelloWorldModule : IHttpModule
{
public String ModuleName
{
get { return "HelloWorldModule"; }
}
// In the Init function, register for HttpApplication
// events by adding your handlers.
public void Init(HttpApplication application)
{
application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
application.EndRequest += (new EventHandler(this.Application_EndRequest));
}
// Your BeginRequest event handler.
private void Application_BeginRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
context.Response.Write("<h1><font color=red>HelloWorldModule: Beginning of Request</font></h1><hr>");
}
// Your EndRequest event handler.
private void Application_EndRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
context.Response.Write("<hr><h1><font color=red>HelloWorldModule: End of Request</font></h1>");
}
public void Dispose()
{
}
}
}
3.配置web.config,msdn上这个说的不清楚,有一些迷惑性:
<
system.web
>
< httpModules >
< add name = " HelloWorldModule " type = " WebApplication1.HelloWorldModule, WebApplication1 " />
</ httpModules >
</ system.web >
< httpModules >
< add name = " HelloWorldModule " type = " WebApplication1.HelloWorldModule, WebApplication1 " />
</ httpModules >
</ system.web >
这里面需要说明一下,其中,type="类名字,程序集名"
这样程序就可以运行了
默认的WebForm1.aspx显示如下:
生成的Html代码如下
<
h1
><
font
color
=red
>
HelloWorldModule: Beginning of Request
</
font
></
h1
><
hr
>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
< HTML >
< HEAD >
< title > WebForm1 </ title >
< meta name ="GENERATOR" Content ="Microsoft Visual Studio .NET 7.1" >
< meta name ="CODE_LANGUAGE" Content ="C#" >
< meta name ="vs_defaultClientScript" content ="JavaScript" >
< meta name ="vs_targetSchema" content ="http://schemas.microsoft.com/intellisense/ie5" >
</ HEAD >
< body MS_POSITIONING ="GridLayout" >
< form name ="Form1" method ="post" action ="WebForm1.aspx" id ="Form1" >
< input type ="hidden" name ="__VIEWSTATE" value ="dDwtNjU0MzcyMTk1Ozs+66KQCSzZjQxcrFSOl6dagrRvRKU=" />
< FONT face ="宋体" ></ FONT >
</ form >
</ body >
</ HTML >
< hr >< h1 >< font color =red > HelloWorldModule: End of Request </ font ></ h1 >
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
< HTML >
< HEAD >
< title > WebForm1 </ title >
< meta name ="GENERATOR" Content ="Microsoft Visual Studio .NET 7.1" >
< meta name ="CODE_LANGUAGE" Content ="C#" >
< meta name ="vs_defaultClientScript" content ="JavaScript" >
< meta name ="vs_targetSchema" content ="http://schemas.microsoft.com/intellisense/ie5" >
</ HEAD >
< body MS_POSITIONING ="GridLayout" >
< form name ="Form1" method ="post" action ="WebForm1.aspx" id ="Form1" >
< input type ="hidden" name ="__VIEWSTATE" value ="dDwtNjU0MzcyMTk1Ozs+66KQCSzZjQxcrFSOl6dagrRvRKU=" />
< FONT face ="宋体" ></ FONT >
</ form >
</ body >
</ HTML >
< hr >< h1 >< font color =red > HelloWorldModule: End of Request </ font ></ h1 >
可以看出我们的HttpModule在页前面和后面各加了一行。