Castle Routing Module
In previous post I was talking about the beauty of MR Controller which I also mention about Routing feature of Castle MonoRail Framework. Today I am going to walk you through basic steps to configure environement, define and register MR Routing.
Step 1: Start with web.config by adding the following line into system.web\httpModules section
<add name="routing" type="Castle.MonoRail.Framework.Routing.RoutingModuleEx, Castle.MonoRail.Framework" />
Step 2: Define routing rules, base on Hammett suggestion I am going to create a static class for handling this task
// filename: RoutingRules.cs
using Castle.MonoRail.Framework.Routing;
public static class RoutingRules
{
public static void Register(IRoutingRuleContainer rules)
{
rules.Add(new PatternRoute("<controller>/[action]")
.DefaultForArea().IsEmpty
.DefaultForAction().Is("index"));
// you can add as many rules as you want
}
}
Step 3: Register rules, this have to be done once and the best place for that is in Global.asax using its’ Application_Startup event
public class Global : HttpApplication
{
public override void Application_Start(object sender, System.EventArgs e)
{
base.Application_Start(sender, e);
RoutingRules.Register(RoutingModuleEx.Engine);
}
}
And that about it. Oh by the way if you are like me. I am a big fan of UrlHelper class and I’m addicted to this macro:
// generate url /product/list<.extension>
${Url.For({@controller: 'product', @action: 'list'})}
And if you do not want to gernerate url with extension /product/list then you have to take another step by adding the following line to monorail configuration section in the web.config
Step 4: Remove extension from generated url
<monorail defaultUrlExtension="">
<!-- other configuration removed -->
<url useExtensions="false"/>
<!-- other configuration removed -->
</monorail>
Now do you have any specific routing rules wanted to discuss? It’s time to dig in ;)
Ken Egozi also had an interesting post related to this topic.

Hi,
How do you make Monorail work without using extensions for actions?
I tried creating some rewrite rules, with Ionics (http://www.codeplex.com/IIRF/) which has mod_rewrite syntax, but I could not make it work.
Dan
October 27, 2008 at 10:55 pm
Dan, are you trying to configure it on IIS? or in development environment?
For IIS it required IIS 6 or higher and you don’t need additional tool to make it works all you have to do is configure wildcard handler aspnet_isapi.dll
Chorn Sokun
October 28, 2008 at 8:14 am
the configuration is done at IIS 5.1/6 level, but i was hoping to avoid wildcard mapping to aspnet_isapi.dll.
i’d hoped i can do something like this:
http://groups.google.com/group/castle-project-devel/browse_frm/thread/7b5f01be7bb6ae1e
<![CDATA[ /product/(?\d+)/show ]]>
Dan
October 28, 2008 at 2:29 pm
@Sokun, I’m trying to make the routing module works with default action or controller, but it doesn’t. I always face with 404 page not found. Did I forget to do something? I really like routing in ASP.NET MVC feature, but I’m not sure I could do the same in MR. I’m using IIS7 with the build from castle trunk for .NET 3.5.
Samnang
November 14, 2008 at 3:51 pm
@Samnang, check this blog post http://hammett.castleproject.org/?p=283
Chorn Sokun
November 14, 2008 at 4:25 pm