Here are some follow-up links and answers to questions from our geekSpeak November 28th on HTTP Handlers and Modules with Chris Love. You can catch the recording on Channel 9 here - geekSpeak recording - httpHandlers and httpModules with Chris Love
An end-to-end IIS extensibility demo - http://www.iis.net/articles/view.aspx/IIS7/Extending-IIS7/Getting-Started/An-End-to-End-Extensibility-Example-for-IIS7-Devel
Documentation on the iHTTPModule interface http://msdn2.microsoft.com/en-us/library/system.web.ihttpmodule(vs.80).aspx
The link to the blog post with the downloads from the demos http://professionalaspnet.com/archive/2007/11/26/GeekSpeak-on-httpHandlers-and-httpModules.aspx
Nathan Good’s RegEx book http://www.apress.com/book/view/1590594975
Zain Naboulsi’s in-depth webcast series on RegEx http://blogs.msdn.com/zainnab/pages/webcasts-page.aspx
Unanswered questions from the webcast:
How do you process other http verbs outside of get and post, can you do that with asp.net pages or do you need a handler?
Yes, this is the documentation on the httpHandlers element, http://msdn2.microsoft.com/en-us/library/aa903367(VS.71).aspx.
The add sub element, which allows for a comma separate list of verbs, http://msdn2.microsoft.com/en-us/library/7d6sws33(VS.90).aspx.
A good common example:
<add verb="GET,HEAD, PUT" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
When should we create a HttpModule vs. adding code to the Global.asax? Pros/Cons?
The first rule I look at is do I want to reuse this code across multiple sites, if yes, then I want a custom httpModule in a class library. Then I have to ask if the Global.ASAX provides access to the event I want to intercept or if I can actually write the best code in the global file. Often I will start working the code in the global file and eventually move it to a custom handler. I believe handlers actually perform better (however we are talking milliseconds) than the global file. I would not look down at one who just used the global file for all module routines!
One can make endless settings in web.config. Is there a reference of what to and how to configure in web.config?
Configuration section reference - http://msdn2.microsoft.com/en-us/library/system.configuration.iconfigurationsectionhandler(VS.90).aspx
Can HttpModule across multiple WebApps on same server?
Make them in a class library and add it to the GAC, then register the module or handler in the web.config file.
Does the http module need to be thread safe, and does it stay in memory ready to run - can the live be specified?
Well by default it is placed in memory the first request made to the site. The methods are tied to the thread processing the request, which handles the threading for us. Now you could run into the scenario where you have an application variable that can be touched by each request. A counter would be common and I would lock that variable when changing the value.
Where should I look if the HttpContext from the Web application is not being passed to an HttpHandler?
It is passed by default. The ProcessRequest method is called to when the handler is invoked and it takes a reference to the current httpContext, so from there you can access anything you need.