Create a REST based HttpHandler in C#

Ancient Knowledge

This article is getting old. It was written in the ancient times and the world of software development has changed a lot since then. I'm keeping it here for historical purposes, but I recommend you check out the newer articles on my site.

Anytime I start a new project I’m usually super pumped to dive into some code and get something done. There I am, a cup of coffee and three energy drinks later, translating the latest business requirement into something simply amazing… or more likely, just another contact management application for the CEO of unnamed company X. But I’m happy, delightfully punching keys on the keyboard, slinging abstractions and enterprise patterns like its just another day. Just one more look at the functional spec and, wait, what is this? That’s right, I just realized that this is not a functional spec at all. This is Functional Spaghetti.

I know you have seen this one, it typically creeps in after someone has done a technical review of the specification, and it goes a little something like this:

  • Company contacts will be stored in our expensive fancy-named data server.
  • The application should access this data using the existing web services.

Let’s see here, we have code reuse, separate databases, this sounds great, let’s get going.

  • Requests sent to the web service must be authenticated by sending a carrier pigeon to the home office and awaiting its return. note: this should be implemented with some sort of thread sleep.

Cue the dramatic music and the rest is history. This is how the following code came about. I’m not going to say this is an ideal solution to anything other than it might solve that one ridiculous software requirement you may have and allow you to keep on trucking. In this short snippet, I’m going to lay out a quick an easy way to make a more flexible HttpHandler. If your like me, you probably don’t ever want to have to create an HttpHandler that has the same basic functionality of an MVC controller, but if you need it, this is a great place to start. Here’s how it works:

  • Start with registering an HttpHandler in the appropriate place in your web.config file that allows a wildcard name.
  • Inherit the RestHttpHandler as a base class for your own implementation and add appropriate actions that return an ActionResult.
  • Dance.

This allows you to to vary the name for the web request and the handler will execute a different action based on the request url. Make note that this line should be changed to reflect the actual assembly names you are using. caution Make sure your routing configuration supports whatever name you choose for the handler extension. Inherit the RestHttpHandler as a base class for your own implementation and create whatever methods you would like to use.

using System;
using System.Collections.Specialized;
using RestHttpHandler;
using RestHttpHandler.Attributes;

public class DemoHttpHandler : RestHttpHandler
{
    [HttpGet]
    public ActionResult Customer()
    {
        return new ContentResult("<h1>John Doe</h1>");
    }

    [HttpPost]
    public ActionResult Customer(NameValueCollection form)
    {
        return new ContentResult("<span>You did it!</span>");
    }

    [HttpGet]
    public ActionResult SendMeToStackOverflow()
    {
        return new RedirectResult("http://www.stackoverflow.com/");
    }

    /// 
    /// Returns the current Type
    /// 
    protected override Type GetSubType()
    {
        return this.GetType();
    }
}

Now you can punch in the url you need into your browser to access the method specified above. For example, the following url:

<a href="http://www.yourdomain.com/sendmetostackoverflow.axd">http://www.yourdomain.com/sendmetostackoverflow.axd</a>

Will call the same named method in the HttpHandler and redirect your browser appropriately. Fork the code on Github and get back to solving those crazy requirements! https://github.com/carbonrobot/RestHttpHandler