|
|||||||||||||
|
HttpModulethe Filter for all Requests
The HttpModule does not replace the target of a request, however the HttpModule receives notification at various processing points during the lifespan of a request. Since (as is the case with HttpHandlers) we can map an HttpModule to all application request pages we can use an HttpModule as the foundation for our web application controller. Let’s start off by taking a look at the IHttpModule interface.
The first thing to notice about this
interface is the lack of an IsPoolable property. There is a
very good reason for this. Unlike HttpHandlers, which are
instantiated for each concurrent request, only one instance
of any given HttpModule is instantiated for any given
application. Naturally, this makes the issue of pooling
irrelevant.
Here’s what this does. After this web site is started, an
instance of FPSNow.WebFilter (an HttpModule class) will be instantiated on the first
call to a .aspx or .asmx
page (or any extension mapped to aspnet_isapi.dll). For
the entire duration of the application, this single class
instance will filter all requests to all .aspx and .asmx pages
on my site.
To recap, a single class instance of my HttpModule controller
class (FPSNow.WebFilter) will filter every single request to
my application pages (HttpHandlers). Let’s continue to
explore the HttpModule processing model in order to see how
we’ll be able to get any meaningful work done under these
circumstances.
As you can see, the application context of the current application is passed in as a single parameter. Let’s take a look at the HttpApplication object in the object browser.
As you can see, the HttpApplication object exposes some very interesting events which we can immediately see are key points in the lifespan of any HTTP request. In order for our HttpModule class to
filter (i.e., get involved with) request processing, all we need to do is to provide event handlers for those events which we wish to monitor, and then wire-up our event handlers to the HttpApplication provided events.
Handlers for these events are defined in the WebFilter class as follows:
In the Init method, which is called when the HttpModule is first instantiated, these event handlers are 'wired up’ to the Application events. The code statements below demonstrate how to associate an event handler with an event. Since the context parameter to the Init method represents the Application object, the following statements associate the HttpModule's event handler to the event provided by the Application object:
Basically, every page on the FPSNow! web
site is comprised of four sections; Header, NavBar, Content and Footer. When a request occurs, the first handler (which I’ve established) to receive notification is OnBeginRequest. At this point, the header
and dynamic NavBar are assembled for the current request and written into the Response buffer.
Then control passes to the .aspx HttpHandler target of the current request. The output from the request handler is added into the same Response buffer. Finally, when the HttpHandler is finished, the next handler, OnPostRequestHandlerExecute is notified. At this point, the footer for the current request is assembled and written into the Response stream. Finally, the entire Response stream for the current request is sent back to the browser.
|
|||||||||||||
|