At this point, we have a working comment tracking control, but its usefulness is limited in any real-world development. Adding the control to all the ASP.NET pages in a large application can take a lot of time. You would also need to remove the pages before making your application go live, which could potentially add errors to previously tested code. What we need is a way to add the control to every page automatically. This is where HttpModules come in. An HttpModule is a class that, for every page, allows you to handle events that are fired at various stages of the creation of the page. The .NET framework uses HttpModules for authenticating users and managing the session state. The rest of this chapter will show you how to write an HttpModule to manipulate the output of any arbitrary aspx file. IHttpModule InterfaceTo write an HttpModule, you need to write a class that implements the IHttpModule interface. The IHttpModule interface is quite simple with only two methods: Dispose() and Init(). For most modules, only the Init() method is of interest, but if you allocate any unmanaged resources that are of module scope, you should free them in the Dispose() method. The Init() method is where you normally attach an event handler to any events that you wish to handle. You should be careful about allocating resources in the Init() method because anything allocated and assigned to a class variable will have application scope. This is because HttpModules are created when the application is initialized and destroyed after the application has completed. EventsThere are a number of events that you can handle with an HttpModule. Table 15.16 describes the different events.
If you need to terminate the processing of a page after handling an event, you should call the CompleteRequest() method of the HttpApplication class. If the CompleteRequest() method is not called, then normal processing will continue after your event completes. Basic HttpModuleLets now take a look at a simple HttpModule. A complete module will do two things. First, it will implement the IHttpModule interface. Second, it will handle one or more of the events listed in Table 15.16. If it did not handle any of the events, it would never be able to do anything useful. Listing 15.7 shows a basic HttpModule. Listing 15.7 C# Code for a Minimum Implementation HttpModulepublic class MyHttpModule : IHttpModule { // Constructor public MyHttpModule() { } // Implement IHttpModule Methods public void Init(HttpApplication context) { context.Error += new EventHandler(OnError); } public void Dispose() { } // Implement the event handler. public void OnBeginError(Object sender, EventArgs e) { HttpApplication app=sender as HttpApplication; // Do something here } } ConfigurationAfter an HttpModule has been written, it needs to be registered so that it can be loaded when the Web application starts. To do so, add an entry inside the <HttpModules> section of either the web.config file or the machine.config file. Adding the entry to the web.config file causes the module to load when a specific Web application starts. Adding the entry to the machine.config file causes the module to load when any Web application starts. The format of entries in the <HttpModules> section is as follows: <configuration> <system.web> <httpModules /> </system.web> </configuration> An <add> entry adds a new HttpModule; a <remove> entry will remove an inherited HttpModule (possibly from a higher level web.config or from machine.config); and a <clear> entry will remove all HttpModules. You will need an <add> entry before your module can be used. If you added a module in the machine.config file, you can remove it in the web.config file of a specific Web application using a <remove> entry in the Web application's web.config file. This allows you to load an HttpModule for every Web application except ones that have a <remove> entry in the web.config file. |