我们可以使用DynamicModuleUtility.RegisterModule(typeof (SomeHttpModule))
以编程方式添加HttpModules - 有没有办法删除它们?
答案 0 :(得分:-1)
Init
Global.asax
方法中实例化HTTP模块
Module.Init(
)
Init
方法中,挂钩所需的事件处理程序。Dispose
方法并取消挂钩
事件处理器。将实例公开为global.asax
上的公共属性,以便您
如果要取消注册模块,可以调用Dispose
// Global.asax
public IHttpModule MyModuleInstance {get;私人集; }
public override void Init()
{
base.Init();
MyModuleInstance = new MyModule();
MyModuleInstance.Init(this);
}
// MyModule.cs
public void Dispose()
{
_context.BeginRequest -= context_BeginRequest;
}
public void Init(HttpApplication context)
{
_context = context;
context.BeginRequest += context_BeginRequest;
}
private void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
app.Context.Response.Write("Hello from OnBeginRequest in custom module.<br>");
}
//取消注册
protected void Button1_Click(object sender, EventArgs e)
{
this.ApplicationInstance.MyModuleInstance.Dispose();
}