Defining ASP.NET MVC Routes and Views in IronRuby, mvc, code 0 comments suggest edit

In a recent post I expressed a few thoughts on using a DSL instead of an XML config file. I followed that up with a technical look at monkey patching CLR objects using IronRuby, which explores a tiny bit of interop.

These posts were precursors to this post in which I apply these ideas to an implementation that allows me to define ASP.NET MVC Routes using IronRuby. Also included in this download is an incomplete implementation of an IronRuby view engine. I haven’t yet implemented layouts. Download (4.93 MB)

This implementation works with the latest CodePlex drop of MVC.

To use routes written in Ruby, reference the IronRubyMvcLibrary from your MVC Web Application and import the IronRubyMvcLibrary.Routing namespace into your Global.asax code behind file. From there, you can just call an extension method on RouteCollection like so…

public class GlobalApplication : System.Web.HttpApplication
  protected void Application_Start()

This will look for a Routes.rb file within the webroot and use that file to load routes. Here’s a look at mine:

$ "products/{action}/{id}"
  , {:controller => 'products', :action => 'categories', :id => ''}
$ "{controller}/{action}/{id}", {:id => ''}
$ "{controller}", {:action => 'index'}, {:controller => '[^\.]*'}
$ "default.aspx", {:controller => 'home', :action => 'index'}

That’s it. No other cruft in there. I tried experimenting with lining up each segment using tabs so it looks like an actual table of data, rather than simply code definitions.

Also included in this download is a sample web app that makes use of the IronRubyViewEngine. You can see how I applied Monkey Patching to make referencing view data cleaner. Within an IronRuby view, you can access the view data via a global variable, $model. The nice part is, whether you pass strongly typed data or not to the view, you can always reference view data via $model.property_name.

In the case where the view data is a view data dictionary, this will perform a dictionary lookup using the property name as the key.

Be sure to check out the unit tests which provide over 95% code coverage of my code if you want to understand this code and improve on it. Next stop, Controllers in IronRuby…

Technorati Tags: ironruby,ruby,aspnetmvc,aspnet,dsl

Found a typo or error? Suggest an edit! If accepted, your contribution is listed automatically here.



5 responses

  1. Avatar for Javier Lozano
    Javier Lozano April 21st, 2008

    two words: love it
    I will use this as the base for the ruby controller... :)

  2. Avatar for Joshua Flanagan
    Joshua Flanagan April 22nd, 2008

    First, I enjoy this type of experimentation and like the direction you are going.
    Ruby code does tend to have less cruft, but I wouldn't say no cruft. You may become immune to it (as C# coders become immune to semi-colons) - but look again and you will notice all of the extraneous $,:,{},> symbols in your ruby example (I know they aren't extraneous to the compiler).
    I'm sure your ruby routing library allows for more advanced scenarios, but the examples you provided can be very easily emulated with a simple extension method:
    static class ReduceCruftWithCSharp {
    public static void Map(this RouteCollection routes, string url, object defaults) {
    routes.Add(new Route(url, new RouteValueDictionary(defaults), new MvcRouteHandler()));
    Now each of your C# route lines will be about the same length as the equivalent ruby line (the C# code starts winning as you add more defaults). So, in this case, it isn't even "less" cruft - its just "different" cruft.
    $ "default.aspx", {:controller => 'home', :action => 'index'}
    Routes.Map("default.aspx", new {controller = "home", action = "index"});

  3. Avatar for Joe Fiorini
    Joe Fiorini August 26th, 2008

    Hey Phil,
    I love your IronRuby samples! Thanks for spending the time to get these working for us!
    One thing I noticed with your RubyRouteLoader is that I had to restart IIS7 every time I changed the routes file. I moved the call into Application_BeginRequest and made some slight modifications to your code to make it work. I posted the details on my blog at I hope you can find this useful!

  4. Avatar for Murph
    Murph March 2nd, 2010

    The download file seems to be corrupted. I only get 102 KB, and it errors when I try to open it.

  5. Avatar for Ken
    Ken October 1st, 2014

    Was the corrupted files problem fixed?