Chorn Sokun's Weblog

Conquer inner fear, push it to the limit!

Posts Tagged ‘RoutingModuleEx

Castle Routing Module

with 5 comments

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.

Advertisements

Written by Chorn Sokun

September 24, 2008 at 11:09 pm