Using HttpHandlers with mod_mono

Mike Roberts’ post about disposing with Web Forms piqued my curiosity about HttpHandlers. I knew what they were, but I’d never considered using them over Web Forms for standard web pages.

As Mike says, you lose all the power (and complexity) that System.Web.UI.Page provides but that could be taken as both a good and/or bad thing.

Not content with just playing with a new feature, I decided to do it on Mono and based on the various instructions I’ve seen for ASP.NET/IIS usage of HttpHandlers, they are essentially the same. Speaks volumes for the Mono project.

Here I’ll explain the steps you should follow to repeat my experiment.

First We’ll write the handler, then we’ll configure ASP.NET to use it, then we’ll configure Apache to handle it correctly, then finally we’ll see it in action.

Step 1 – Writing the handler

HttpHandlers are classes that implement the IHttpHandler or IHttpAsyncHandler interfaces. For this exercise we’re only interested in IHttpHandler, and for that we need to implement the Public IsReusable property (typically True) and ProcessRequest method.

Here’s the code for our handler (SimpleHandler.cs):


 using System;
 using System.Web;

 public class SimpleHandler : IHttpHandler
 {
   public void ProcessRequest(HttpContext Context)
   {
     Context.Response.Write("Hello World!");
   }

   public bool IsReusable
   {
     get{ return true; }
   }
 }

Simple, eh? Now compile it with:


 $mcs -t:library SimpleHandler.cs

Copy the resulting file (SimpleHandler.dll) to your bin folder within your web application.

Configuring ASP.NET

We’ll need to edit our web.config to handle our handler. First step is to decide what sort of pages (.html,.aspx etc.) we’re going to handle. Since our handler is called SimpleHandler, let’s handle .simple files. Here’s a complete web.config to handle this.


 <?xml version="1.0" encoding="utf-8" ?>
 <configuration>
         <system.web>
                 <httpHandlers>
                         <add verb="*" path="*.simple" type="SimpleHandler,SimpleHandler" />
                 </httpHandlers>
         </system.web>
 </configuration>

See here for an explanation of the &lt;httpHandlers&gt; element.

Configuring Apache

OK, we’ve got our handler, and we’ve configured ASP.NET to use our handler for pages ending with .simple extension. Now we need to instruct Apache1 to use mod_mono for .simple pages. Simply add the following to your Apache configuration file:


AddHandler mono .simple

Restart Apache, and browse to any .simple page (e.g. http://server/foo.simple). Because of the way we’ve configured it, any page requested with a .simple extension will be handled by our handler.

All talk, no action

Not true at all. Click here or here to see it in action.

Of course we can do something more impressive than that!

I’ve set up a second handler – PageHandler – which handles .page pages. It uses template4dotnet to provide the static content/formatting, and the handler provides the dynamic content. Go look.

I would need to do some performance testing, but I would definitely consider using HttpHandlers (with some form of templating system) for creating sites. There’s still very much a place for Web Forms, but now I recognise they’re just one facet of ASP.NET.


1 Although IIS has a different way of doing it, you’re still performing the same action – instructing the web server to use a specific program to process specific page types.

Comments are closed.



Mobilized by Mowser Mowser