/* This file demonstrates as very simple HttpModule. You can use an HttpModule to do all kinds of things, but usually they are used to control access to a system (though that's not their only use). An HttpModule is created by creating a class that implements the System.Web.IHttpModule interface. You implement an HttpModule by adding the HttpModule to the httpModules section of the system.web section of the web.config file. In this example you can see that when the HttpModule is called, the 'BeginRequest' event of the 'HttpApplication' is set to an anonymous delegate. In the event handler, you can see that I'm casting the sender as 'HttpApplication' and then doing a null check. If the null check passes, then you can do all kinds of things. Perhaps you want to block all traffic from a particular site or perhaps you want to implement your own security model. This is where you could do it. You can handle many HttpModules in a system and the standard object-oriented principles of high cohesion apply to these as well. Your HttpModules should do one thing and do it well. Don't create an HttpModule that does all kinds of things simply for the sake of centralized management. */ using System; using System.Web; namespace Sample.Web.HttpExtensions { public class SampleHttpModule : IHttpModule { public void Dispose( ) { } public void Init(HttpApplication context) { context.BeginRequest += delegate(Object sender, EventArgs ea) { HttpApplication ha = sender as HttpApplication; if (ha != null) { /* * You may do any processing here. Perhaps you want to implement a new security model? * Or maybe you want to block a range of IP addresses? * */ } }; } } }