Dynamically register httpmodule in Asp.net 4.0

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.