IronRuby ASP.NET MVC With Filters
One 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
Ivan Porto Carerra has taken this prototype and is running with it. To download the latest, check out his IronRubyMVC GitHub project.