The Dynamic Registration of httpmodule makes it unnecessary to configure httpmodel in the configuration file to avoid too many configuration errors. Let's take a look at how to dynamically register httpmodule.
First, we implement the custom httpmodel
Public Class Custommodule: ihttpmodule
{
Public Void Dispose ()
{
// Nothing to do here
}
Public VoidInit (httpapplication context)
{
Context. beginrequest+ =(Sender, E)=>Processcookie (httpapplication) sender );
}
}
}
Second, create a static class, name it preapplicationstartcode, and add a static method prestart ()
Public Class Preapplicationstartcode
{
Private Static Bool _ Isstarting;
Public static void prestart ()
{< br> If ( ! _ isstarting)
{< br> _ isstarting = true ;
//Note the dynamic registration here. This static method is available in Microsoft. Web. Infrastructure. dynamicmodulehelper
Dynamicmoduleutility. registermodule (Typeof(Custommodule ));
}
}
}
Note. We recommend that you use preapplicationstartcode in the class name list. I have not tested it. You can test it with other class names.
Three, registered in properties/assemblyinfo. CS
[Assembly: preapplicationstartmethod ( Typeof (Mytest. preapplicationstartcode ), " Prestart " )]
In these three steps, your httpmodule does not needProgramConfigured. This method is suitable for registering httpmodule when developing component DLL. Microsoft's Asp.net mvc3 uses dynamic httpmodule. If you are interested, you can check the source code.