Grab it by the Handlers

by Chris 5/6/2008 4:45:00 PM

handlebar I have been looking more closely at how BlogEngine.NET uses httpHandlers.  This was a bit of a mystery to me so I decided to dive head first into how httpHandlers work.  I found an excellent article that talks at length about what is a httpHandler, and another web site that automatically helps you build a template for a httpHandler. If you aren't familiar with what a Web Handler is then I would recommend having a look at your ~/web.config file.  In this file you will discover a section named <httpHandlers> that has several entries:

<httpHandlers>
  <add verb="*" path="file.axd" type="BlogEngine.Core.Web.HttpHandlers.FileHandler, BlogEngine.Core" validate="false"/>
  <add verb="*" path="image.axd" type="BlogEngine.Core.Web.HttpHandlers.ImageHandler, BlogEngine.Core" validate="false"/>
  <add verb="*" path="syndication.axd" type="BlogEngine.Core.Web.HttpHandlers.SyndicationHandler, BlogEngine.Core" validate="false"/>
  <add verb="*" path="sitemap.axd" type="BlogEngine.Core.Web.HttpHandlers.SiteMap, BlogEngine.Core" validate="false"/>
  ...
</httpHandlers>

You might recognize a couple of these handlers such as syndication.axd and sitemap.axd.  Essentially httpHandlers respond to web requests whenever they are directly called by the browser (ex. http://www.dscoduc.com/sitemap.axd).  Each of the handlers map to a class file in the BlogEngine.DLL file and performs a specific task when called.  As you might expect the sitemap.axd httpHandler dynamically builds a valid sitemap xml data result that a search engine like Google can consume.  Using a httpHandler instead of a static XML file is a powerful feature as you don't have to manually manage the content of the data provided in the sitemap.xml file.  As you can see BlogEngine.NET has several built-in httpHandlers to make things simple for the blog administrator.

Another excellent side-effect of using a httpHandler is the ability to perform additional tasks before serving up the requested data. 

Take, for example, the ability to include EventHandlers within the httpHandler.  An EventHandler is a hook-in that allows for code to be executed at different stages of the httpHandler execution.  Depending on how the httpHandler was written this could include steps that validate if the httpHandler should complete or not. 

Several people (including me) have released a file download counter that records the number of downloads from your blog.  These extensions leverage the EventHandlers found in the fileHandler.cs file without which this function would be much more difficult. 

Another example is the ability to cache the content that is being served up before it is delivered to the requesting party.  If the requested information is found in the cache then an expensive file level request is avoided.  Take for example the js.axd httpHandler.  This handler calls the javascript file passed in the arguments string and loads it into the cache, compresses and cleans up the contents, and then serves it to the requestor.  The next time the javascript is requested the contents is quickly pulled out of memory instead of the disk.

After my study I can say that the real take away from this (long winded) blog entry is that you should really explore the use of httpHandlers.  When ever you are accessing javascript, images, or css files you should really look towards using the httpHandlers in order to reduce the number of file level reads.  The syntax is easy really: here are some examples that I used in my SyntaxHighlighter extension:

  private string ResolveScriptUrl(string url)
  { return Utils.RelativeWebRoot + "js.axd?path=" + Utils.RelativeWebRoot + url; }

  private string ResolveCssUrl(string url)
  { return Utils.RelativeWebRoot + "css.axd?name=" + Utils.RelativeWebRoot + url; }

Please let me know if you have any questions or issues with using httpHandlers in your development.

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
Loading



Powered by BlogEngine.NET Theme by Mads Kristensen Hosted by 1and1 Hosting Sign in
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way. © 2008 Chris Blankenship

Welcome to my blog

Welcome to my site

Villainy wears many masks,
none of which so dangerous as virtue…



Subscribe to comments feed Recent comments exp/col

View Chris Blankenship's profile on LinkedIn   DSCODUC on Technorati

check out my neighbors in meatspace  

Stop Spam Harvesters, Join Project Honey Pot   This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License