usingSystem;usingsystem.web;usingMicrosoft.Web.Infrastructure;namespacemvcapplication1{ Public classCustommodule:ihttpmodule { Public voidInit (HttpApplication context) {context. BeginRequest+=NewEventHandler (context_beginrequest); } voidContext_beginrequest (Objectsender, EventArgs e) {HttpApplication AP= Sender asHttpApplication; if(AP! =NULL) {ap. Response.Write ("Uncle Tom tested Preapplicationstartmethod through! <br/>"); } } Public voidDispose () {// Nothing } } Public classPreapplicationstartcode {Private Static BOOLhasloaded; Public Static voidPrestart () {if(!hasloaded) {hasloaded=true; //Note The dynamic registration here, this static method in Microsoft.Web.Infrastructure.DynamicModuleHelperMicrosoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule (typeof(CustomModule)); } } }}
usingSystem.Reflection;usingSystem.Runtime.CompilerServices;usingSystem.Runtime.InteropServices;//General information about a assembly is controlled through the following//set of attributes. Change these attribute values to modify the information//associated with an assembly.[Assembly:assemblytitle ("MvcApplication1")][assembly:assemblydescription ("")][assembly:assemblyconfiguration ("")][assembly:assemblycompany ("")][assembly:assemblyproduct ("MvcApplication1")][assembly:assemblycopyright ("copyright©2014")][assembly:assemblytrademark ("")][assembly:assemblyculture ("")]//Setting ComVisible to False makes the types in this assembly not visible//To COM components. If you need to access a type in this assembly from//COM, set the ComVisible attribute to true on that type.[Assembly:comvisible (false)]//The following GUID is for the ID of the typelib if this project is exposed to COM[Assembly:guid ("98a0cfd0-fae7-43dd-8626-b49f8df939be")]//Version information for a assembly consists of the following four values:////Major Version//Minor Version//Build Number//Revision////You can specify all the values or you can default the Revision and Build Numbers//by using the ' * ' as shown below:[Assembly:assemblyversion ("1.0.0.0")][assembly:assemblyfileversion ("1.0.0.0")][assembly:system.web.preapplicationstartmethod (typeof(Mvcapplication1.preapplicationstartcode),"Prestart")]
Dynamically registering HttpModule in ASP. NET 4.0