LA.NET [EN]

Jul 23

As we’ve seen in the previous post, by default you’ll end up using the MvcRouteHandler class for setting up the handler that will return the HTTP response to the client. As you can see (by looking at the code), the GetHttpHandler will only return a new MvcHandler and pass it the current RequestContext. If this class is really simple, then why am I talking about it here? I’m doing that because this is the first class of the MVC pipeline that you can replace if you’re not interested in using the default MVC handler.

When will you want to replace this class? Whenever you think that having a “simple” handler is more than enough for a specific request. For instance, if you think that creating an MvcHandler is just too much for getting the bytes of an image that are stored on the db or you just want to reuse and old handler you’ve built for that, then you can simply create your own IRouteHandler class and implement it so that it returns the handler you want.

A quick example should make this really easy to understand. Lets suppose you’ve got a ImageHandler class that looks like this:

public class ImageHandler:IHttpHandler
{
  //code that implements the interface and gets the image from
  //the database
  //note: remember that you can receive an instance of the RequestContext 
  //class so that you can easilly recover
  //the parameters from a user-friendlier url.
  //the best way of passing this RequestContext to your class
  //is (probably) to define a constructor that receives a reference to
  //an item of that type.
  public ImageHandler(RequestContext context)
  {
     //more code
  }
}

Now, you just need to create your own route handler that creates elements of this type. Here’s a simple class that does just that:

public class ImageRouteHandler: IRouteHandler
{
  IHttpHandler IRouteHandler.GetHttpHandler(RequestContext context)
  {
      return GetHttpHandler(context);
  }

  protected virtual IHttpHandler GetHttpHandler(RequestContext context)
  {
     return new ImageHandler( context );
  }
}

Now, the only thing you need is to set it up a route so that the requests get forwarded to our new route handler:

var route = new Route ( “images/{name}”, new ImageRouteHandler() );
routes.Add( “images”, route);

If you really feel like it, you can also add a new extension method that will do that for you:

public static class MyRouteCollectionExtensions
{
   public static void MapImageRoute( this RouteCollection routes, String name,String url)
  {
     var route =  new Route ( url,new ImageRouteHandler() );
     route.Add( name, route);
  }
}

And then, you could simply add a route by writing the following:

routes.MapImageRoute( “images”, “images/{name}” );

where routes is a reference to an existing RouteCollection (which you’ll normally get from the RouteTable.Routes property). You could add more overloads that would handle default values and constraints (for more info on these, check my routing series), but I thought that the current code was more than enough for helping you get started building your own handlers.

And that’s all for today. On the next post, I’ll start looking at the MvcHandler and we’ll see how it accomplishes all the stuff

2 comments so far

  1. Mihai
    10:49 am - 9-4-2008

    Thank you so much for all of this series on MVC

  2. Buy Ambien
    4:38 am - 2-14-2010

    independent chemical scroll orchestrated bldg leaver adjunction department preparing deferred parity
    ambisoltersos makalavertonicos