We can programatically add HttpModules using DynamicModuleUtility.RegisterModule(typeof (SomeHttpModule)) - is there a way to remove them?
Asked
Active
Viewed 1,588 times
1
Matt Hinze
- 13,577
- 3
- 35
- 40
-
Don't directly know of a way to do remove them programmetically. This post might help you out: http://stackoverflow.com/questions/239802/programmatically-register-httpmodules-at-runtime. Isn't it better to check in your custom HttpModule if it should be applied to the current request or not? – Christophe Geers Aug 18 '11 at 13:25
1 Answers
-1
- Instantiate the HTTP Module in
Initmethod inGlobal.asax - Call
Module.Init() as described here - In
Initmethod of your module, hook required eventhandlers. - Override the
Disposemethod in the handler and unhook the eventhandlers. Expose the instance as public property on
global.asaxso that you can callDisposewhen you want to unregister the module// Global.asax
public IHttpModule MyModuleInstance { get; private set; }
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>");
}
// TO unregister
protected void Button1_Click(object sender, EventArgs e)
{
this.ApplicationInstance.MyModuleInstance.Dispose();
}
Community
- 1
- 1
Unmesh Kondolikar
- 9,256
- 4
- 38
- 51
-
1No, Dispose isn't going to work. This is completely specific to an implementation of IHttpModule. I want it removed from the pipeline in the same way DynamicModuleUtility.RegisterModule adds it to the pipeline. Also, the reason I want to remove it is because it's added by default. If I had the ability to add it to my application instance I would just *not* add it. – Matt Hinze Aug 18 '11 at 16:18
-
@Matt - that is strange. Why do you want to remove a module added by someone else? MOreover the DynamicModuleUtility says it is an infrastructure utility and is not intended to be used directly from your code. Do explain the scenario in more details in your question so that it helps people trying to answer. – Unmesh Kondolikar Aug 18 '11 at 16:57
-
Because if you use Razor with ASP.NET MVC 3 you load the FormsAuthentication module. – Matt Hinze Aug 27 '11 at 00:35