IHttpModule 入门
今天去笔试,IHttpModule概念描述一下,悲剧了,以前搞asp.net就没有系统学过,坐项目也没有遇到过,所以就悲剧了,一阵乱写.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
namespace ClassLibrary2
{
//IHttpModule:侦听application事件或用户自定义事件.所谓的监听事件就是指当你的控件发生事件后,就会调用我这个接口里面的方法
//IHttpModule有两个方法,一个是Init(HttpApplication),还有一个是Dispose()
//第一个方法当然是我监听到你的事件发生的时候就初始化一个内容,Dispose()方法就是释放一些内容,就这样想吧
public class Class1:IHttpModule
{
public void Init(HttpApplication context)
{
//HttpApplication,既然是监听application事件的,当然有这个参数,这个容易理解。
//BeginRequest和EndRequest是httpApplication的两个事件,利用委托进行将事件和方法联系起来
//BeginRequest事件是指,当页面开始的时候,这个事件被激发
//EndRequest事件是指,当页面结束的时候,这个事件被激发
context.BeginRequest += new EventHandler(context_BeginRequest);
context.EndRequest += new EventHandler(this.context_EndRequest);
}
public void Dispose()
{
throw new NotImplementedException();
}
public void context_BeginRequest(object obj, System.EventArgs e)
{
HttpApplication app = (HttpApplication)obj;
app.Response.Write("Hi,testing customer http module request");
}
public void context_EndRequest(object obj, System.EventArgs e)
{
HttpApplication app = (HttpApplication)obj;
app.Response.Write("Hi,end request here");
}
}
}
然后生成dll,新建个asp.net应用,引用此dll,在web.config中加入
<httpModules> <add name="test" type="ClassLibrary2.Class1,CustomerHttpModules"/> </httpModules>
则当有页面请求时会查找到此dll,然后执行其中过程。
寻找21世纪的伯牙
浙公网安备 33010602011771号