IronRuby ASP.NET MVC With Filters

asp.net mvc, asp.net, dlr 0 comments suggest edit

Last July, I blogged about an IronRuby ASP.NET MVC prototype Levi and I put together with John Lam and Jimmy Schementi of the DLR team. It was really rough around the edges (and still is!)

IronRuby on ASP.NET MVC
DemoOne of the benefits of doing that prototype was that it inspired all the work around action and controller descriptors in ASP.NET MVC (something I need to write more about later) which decoupled us from exposing reflection in our public API and improved the overall design of ASP.NET MVC greatly. This had the nice side-effect of making the implementation of IronRuby on top of ASP.NET MVC much cleaner.

In this updated prototype, I’ve now implemented support for ASP.NET MVC filters. You can define action filters and authentication filters (I need to test the other filter types). Keep in mind, this is a very rough prototype code still. I’ve just been swamped up to my neck lately and this is a spare-time labor of love.

I’ve only implemented one type of filter so far. You can specify a class to apply to an action method and the class implements a specific filter interface. I haven’t done anything like the more rails-y filter_before and filter_after thing.

Here’s an example of an action filter in IronRuby. This one simply writes something to the response in the before method, and does nothing in the after method.

class MyFilter < IronRubyMvc::Controllers::RubyActionFilter
    def on_action_executing(context)
      context.http_context.response.write 'MyFilter '
    end
    
    def on_action_executed(context)
      # noop
    end
    
    def method_missing(name, *args)
        show_action_info name, args
    end
end

(Gee, I wish I had a ruby syntax highlighter plug-in for WLW)

And here’s the use of that filter within a controller.

require 'HomeModel'
require 'MyFilter'

class HomeController < Controller
  filter :index, MyFilter

  def index
    view nil, 'layout', HomeModel.new
  end  
end

Notice that the way you define a filter on the index action is:

filter :action_name, FilterClassName

In the sample code I uploaded, you can see the effects of the filter at the top of the page. :) Hopefully I’ll find more time to update this, but as I said, it’s a labor of love, but time is in short supply.

In the meanwhile, I also need to look into whether there’s enough interest to make this a CodePlex project. There’s a bit of due diligence I have to do before I put code up on CodePlex, which is why I haven’t done it already because I’ve been busy.

And before I forget, here’s the download location for the sample.

Ivan Porto Carerra has taken this prototype and is running with it. To download the latest, check out his IronRubyMVC GitHub project.

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

Comments

avatar

8 responses

  1. Avatar for Rob Conery
    Rob Conery February 17th, 2009

    Ohhhhh Ruuuuubyyyyyy! Yay! Nice work Phil :). I know Bertrand is going to LOVE seeing Ruby code in MVC. This is good stuff!

  2. Avatar for Saad Savari
    Saad Savari February 17th, 2009

    Looking forward for Ruby MVC store front Rob Conery Style.
    Love Ruby. Good stuff

  3. Avatar for Steve
    Steve February 17th, 2009

    Looking good!
    Sure hope to see these efforts continue!

  4. Avatar for Sergio Pereira
    Sergio Pereira February 17th, 2009

    Not to be "stealing" too much from RoR, but I think you can make the Ruby syntax work even more in your favor if you change the order of the parameters in the filter method:
    filter FilterClassName, :action_name
    Then you could take it further like:
    filter FilterClassName, :only => [:action_1, :action_2]
    # or
    filter FilterClassName, :except => [:action_3, :action_4]

  5. Avatar for haacked
    haacked February 17th, 2009

    @Sergio, I like it! Thanks for the suggestion.

  6. Avatar for Joe Chung
    Joe Chung February 17th, 2009

    Looking forward to seeing this as a CodePlex project! IronRuby + ASP.NET MVC = Crazy Delicious!

  7. Avatar for Jeffrey Richardson
    Jeffrey Richardson February 18th, 2009

    I'm not much into Ruby myself, but this does seem interesting. The more interesting part, however, is how you mention action and controller descriptors. I had worked with them just before you mentioned them on this post, and recently I've made a post of my own about them ( http://wc4f.qsh.es/archive/2009/02/19/6.aspx ). I do look forward to more posts about the extensibility of ASP.NET MVC from both you and ScottGu.

  8. Avatar for Johan Danforth
    Johan Danforth March 21st, 2009

    Oh, this is so sweet. Now if I could just get one of my customers to start a new web project. Most things seems to be "brownfield" or (for me) WCF.
    Please keep up the work on IronRuby + ASP.NET MVC, I seriously think it will be quite popular give it some time and PR :) What might be needed is some more work on the M in MVC, something that plugs in with ASP.NET MVC somehow?