2008-02-03

Changing the URL structure

I wanted the following URL structure in my website

www.mysite.com/product/myproductname/whatsnew

but the default url mapping in MonoRail would translate this as

www.mysite.com/[controller]/[action]/[id]

So it would expect to find this

public class ProductController
{
  public void MyProductName(string id)
  {
  }
}

whereas what I actually want is

public class ProductController
{
  public void WhatsNew(string productName)
  {
  }
}



  1. Open Web.Config

  2. Locate the monorail node

  3. Locate the routing child node

  4. Now add a new <rule> to the top of the list:


<rule>
<pattern>/(product)/(\w+)/(\w+)</pattern>
<replace><![CDATA[ /product/$3.rails?productName=$2]]></replace>
</rule>


As this is at the top of the list it will have the highest priority. If the URL matches /product it will remap the url

From:
www.mysite.com/product/myproductname/whatsnew

To:
www.mysite.com/product/whatsnew.rails?productname=MyProductName


without the user ever seeing it :-)

No comments: