I CAN HAS CODE?!?
May
18
Posted in ASP.NET MVC by Michael at 00:08.

In Ruby on Rails (RoR) there is a method on the controller class called respond_to which, simply put, allows one to easily tailor the response format based on the requested mime type format (through the Accept header or as an extension on the URL) of the request.  There are many reasons why you would to do this, but one of the main ones, in my mind, is that you want to have your controllers be able to work with ‘service’ type calls instead of only requests for pages.  The same controller and action could be used to serve up HTML when a normal request comes through, but when a JSON request (i.e. application/json) comes through, instead of using the standard view and layout, only valid JSON data would be returned.

Now there are a number of ways to implement this functionality but I find the RoR method to be quite clean and succinct, and because of that, when I needed to do something similar with ASP.NET MVC, I modeled my approach after RoR’s.  So, without further ado, let’s jump into the code shall we?

I started with a very simple extension method for the HttpRequestBase class that simply checks to see if one or more strings are in the HTTP Accept header.

public static class HttpRequestBaseExtensions
{
    public static bool CanAccept(this HttpRequestBase request, string[] types, bool exact)
    {
        return Array.Exists(
            request.AcceptTypes, 
            a => (a == "*/*" && exact == false) || Array.Exists(
                types, 
                t => t.Equals(a, StringComparison.OrdinalIgnoreCase)
            )
        );
    }
}

Now, there a number of ways to optimize this, but the above is very strait forward and simple.  We iterate over the values in the Accept header, if the header can accept anything, i.e. ‘*/*’, and we don’t need an exact match, we are done and the request can accept the supplied types.  Otherwise, we look over our supplied types, for a matching one.  Easy. 

Since we can now easily check whether or not a given request can accept a supplied type, it’s time to move onto the meat of the code.  Instead of using a strait comparison between the requested and supplied mime types, I opted to use a more extensible approach.  I figure we could make all of our content ‘checks’ predicates so not only could we check for a given mime type, but we could also check to see if the request was an ajax call, or really anything else we want. 

public static class RequestFormat
{        
    public static Predicate<ControllerContext> Html = (c) => 
        c.HttpContext.Request.CanAccept(new [] { "text/html" }, false);
    public static Predicate<ControllerContext> HtmlAsync = (c) => 
        c.HttpContext.Request.CanAccept(new[] { "text/html" }, false) && c.HttpContext.Request.IsAjaxRequest();
    public static Predicate<ControllerContext> Json = (c) => 
        c.HttpContext.Request.CanAccept(new[] { "application/json" }, true);
    public static Predicate<ControllerContext> Xml = (c) => 
        c.HttpContext.Request.CanAccept(new[] { "application/xml" }, true);        
}

I chose to use ControllerContext as the parameter to the predicate instead of HttpRequestBase because by doing so we can access the route values among other things which would let us check for the format in the URL, for example.

public static class RequestExtension
{
    public static Predicate<ControllerContext> Html = (c) => 
        String.IsNullOrEmpty(c.RouteData.Values["format"] as string);
    public static Predicate<ControllerContext> Json = (c) => 
        "json".Equals(c.RouteData.Values["format"] as string, StringComparison.OrdinalIgnoreCase);
    public static Predicate<ControllerContext> Xml = (c) => 
        "xml".Equals(c.RouteData.Values["format"] as string, StringComparison.OrdinalIgnoreCase);
}

Let’s move to the class that does the majority of the work, the RequestFormatResponder.

public class RequestFormatResponder
{
    protected class SupportedFormat
    {
        public Predicate<ControllerContext> Predicate { get; set; }
        public Func<ActionResult> Result { get; set; }
    }

    protected List<SupportedFormat> Supported { get; set; }

    public RequestFormatResponder()
    {
        Supported = new List<SupportedFormat>();
    }

    public Func<ActionResult> this[Predicate<ControllerContext> predicate]
    {
        set { Supported.Add(new SupportedFormat { Predicate = predicate, Result = value }); }
    }

    public ActionResult Respond(ControllerContext context)
    {
        var match = Supported.LastOrDefault(s => s.Predicate(context));

        if (match != null)
            return match.Result();

        return null;
    }

    public Func<ActionResult> Html { set { this[RequestFormat.Html] = value; } }
    public Func<ActionResult> HtmlAsync { set { this[RequestFormat.HtmlAsync] = value; } }
    public Func<ActionResult> Json { set { this[RequestFormat.Json] = value; } }
    public Func<ActionResult> Xml { set { this[RequestFormat.Xml] = value; } }
}  

There’s really not too much to go over.  We have a child class, SupportedFormat, that contains a supplied predicate and function to return an ActionResult, which is the resultant value should a an acceptable match be found.  On top of that, we have a list to store the formats that are acceptable (order is important), we have a couple ways of setting those supported formats, and we have the function that does all of our processing, Respond.  I’ll get to the two ways of setting supported values in a bit, but it is worth noting that the Respond method implies that the order of the supported formats should be least specific to most specific.

Now, we are almost ready to use our nifty new respond_to implementation.  There is only one more thing to do and that is provide our method on the controller.  I prefer to create a controller class which all of my controllers inherit from in an application (called ApplicationController no less … a hold over from my time with RoR) so I’m going to place our very own RespondTo method there.

public class ApplicationController : Controller
{
    protected ActionResult RespondTo(Action<RequestFormatResponder> block)
    {
        var responder = new RequestFormatResponder();

        if (block != null)
            block(responder);

        var result = responder.Respond(ControllerContext);
        if (result != null)
            return result;           

        throw new HttpException(400, "Unable to respond to requested format.");
    }
}

Overall, it’s pretty simple.  We accept a delegate parameter to which we will pass our responder instance.  We create our responder, call the block, and let our responder do its work.  If the result is non null, we return and go about our way, otherwise we through an exception to return an HTTP status code of 400 (bad request) since the request was for a format we don’t support.

It’s now time for the fun part … putting our code to use!  I merely created a basic ASP.NET MVC project for this part.

public class HomeController : ApplicationController
{
    public ActionResult Index()
    {
        ViewData["Message"] = "Welcome to ASP.NET MVC!";

        return RespondTo(format =>
        {
            format.Html = () => View();
            format.Json = () => Json(new { message = "hello world" });              
        });            
    }

    public ActionResult About()
    {
        return RespondTo(format =>
        {
            format[RequestExtension.Html] = () => View();
            format[RequestExtension.Json] = () => Json(new { message = "about" });
        });
    }
}
/* route to support extension based format */
routes.MapRoute(
    "Default-Extension",                                              
    "{controller}/{action}.{format}/{id}",                  
    new { controller = "Home", action = "Index", id = "" }  
);

Bam!  Here you can see we call our RespondTo method and pass in a lambda expression in which we will tell the responder what formats we support.  In the Index action, I’m using the property setters defined at the bottom of our RequestFormatResponder class which, as you can see, look fairly similar to the RoR method.  The only downside is that you have to add all of the formats that you will support to your responder, but in most cases, that won’t be a big deal.  I the About action, instead of using the property setters, we use the index setter.  It’s pretty similar, though not as clean, in my opinion.  However, you don’t need to extend your responder class at all to add more types.  You could even just specify a new predicate right there if you needed to. 

The rest of that code block should be pretty easy to read.  When a request comes in for HTML, we are simply telling it to return our View, and when a request comes in for JSON, we are telling it to return a JSON serialized object.  The reason we have to use the lambda expressions as values instead of the functions themselves is because we don’t want the actual function executed the correct format has been determined.  If we didn’t do this, both View and Json would be executed, and then the correct one to return would be determined. 

Well, there you have it.  Ruby on Rails like respond_to functionality in an ASP.NET MVC world.  We’ve been using this approach in a project at work and, so far, it’s worked out quite well.  I hope you find it just as useful.

Both comments and pings are currently closed.

3 Responses to “Simple Ruby on Rails respond_to Functionality In ASP.NET MVC”

  1. Andriy Mykhaylyuk Says:

    Thanks for you post, exactly the solution which I was looking for.

  2. Erin Says:

    Have you ever had any issues with Safari or Chrome? I love the concept, but am running into issues with these browsers.

  3. Michael Says:

    I haven’t personally tested this on either, but conceptually, there shouldn’t be any issues. Perhaps the Accept header or the MIME types are being sent across differently? I’ll give it a test when I get some free time.