Smarter Routing

1 view
Skip to first unread message

Hamilton Verissimo

unread,
Oct 5, 2007, 3:42:50 PM10/5/07
to castle-pro...@googlegroups.com
Guys,

I consider the routing support on MR one of its weaknesses. I wish
there was a way to bind a routing rule to the urlbuilder, so that
whenever a url routing rule is changed, it affects redirects and links
generated by the urlhelper (directly or indirectly)

Long time ago I spent some time on this, but the only way I found was
to give more semantics to the routing rule

/products/view/[id]

$Url.Link("view detail", { controller: product, action: view, id:
$product.id } )

Things can get really hairy if you're dealing with a "recursive" path

/products/[category]/.../[subcategory_n]/[productname]

$Url.Link("view detail", { ??? )

Any ideas?

--
Cheers,
hamilton verissimo
ham...@castlestronghold.com
http://www.castlestronghold.com/

Ayende Rahien

unread,
Oct 5, 2007, 3:48:50 PM10/5/07
to castle-pro...@googlegroups.com
A DSL :-)

More seriously, something like:
public interface IRoutingRule
{
     string GetRoutedUrl(string url);
     string CreateUrl(params object[] args);
}

The default impl could be RegEx and string.Format, but then you can also implement it yourself and decide a you want.

Hamilton Verissimo

unread,
Oct 5, 2007, 3:59:27 PM10/5/07
to castle-pro...@googlegroups.com
I'm not sure expression it is the problem... maybe I'm not expression
myself very clearly. I'll think of real world examples.

G. Richard Bellamy

unread,
Oct 5, 2007, 4:06:24 PM10/5/07
to castle-pro...@googlegroups.com
I think one of the implementation details is that a Production level Url
Rewriter needs to be loaded earlier in the pipeline than you can get
from an HttpModule, thus my favoring the ISAPI approach.

Fábio Batista

unread,
Oct 5, 2007, 4:08:58 PM10/5/07
to castle-pro...@googlegroups.com
On 10/5/07, Ayende Rahien <aye...@ayende.com> wrote:
> A DSL :-)
>
> More seriously, something like:
> public interface IRoutingRule
> {
> string GetRoutedUrl(string url);
> string CreateUrl(params object[] args);
> }
>
> The default impl could be RegEx and string.Format, but then you can also
> implement it yourself and decide a you want.

I like that in MR :) We don't force our power-users to study and try
to hack into our "smart" code, they can replace any functionality for
their own.

> On 10/5/07, Hamilton Verissimo < ham...@castlestronghold.com> wrote:
> > Long time ago I spent some time on this, but the only way I found was
> > to give more semantics to the routing rule
> >
> > /products/view/[id]
> >
> > $Url.Link("view detail", { controller: product, action: view, id:
> > $product.id } )

Personally, I started to work with named capture groups on my routing
rules. So instead of:
<pattern>/product/(\d+)/show</pattern>
<replace>/product/show.rails?id=$1&amp;</replace>

I'm using:
<pattern><![CDATA[ /product/(?<id>\d+)/show ]]></pattern>
<replace><![CDATA[ /product/show.rails?id=${id}& ]]></replace>

That makes it easier to inspect the rule and discover its parameter
names. We can also apply names and scopes to the <pattern> tag, for
use with the UrlHelper:

<rule name="showProduct">
<pattern><![CDATA[ /product/(?<id>\d+)/show ]]></pattern>
<replace><![CDATA[ /product/show.rails?id=${id}& ]]></replace>
</rule>

$Url.Link("view detail", { named: "showProduct", id: $product.id } )

<rule name="show" in-controller="product">
<pattern><![CDATA[ /product/(?<id>\d+)/show ]]></pattern>
<replace><![CDATA[ /product/show.rails?id=${id}& ]]></replace>
</rule>
<rule name="show" in-controller="user">
<pattern><![CDATA[ /restricted/user/(?<id>\d+)/show ]]></pattern>
<replace><![CDATA[ /restricted/user/show.rails?id=${id}& ]]></replace>
</rule>

When writing a view for ProductController:
$Url.Link("show product", { named: "show", id: $product.id } )

When writing a view for UserController:
$Url.Link("show user", { named: "show", id: $user.id } )

--
Fábio David Batista
fabio.dav...@gmail.com
http://nerd-o-matic.blogspot.com

Rafael Teixeira

unread,
Oct 5, 2007, 4:12:07 PM10/5/07
to castle-pro...@googlegroups.com
I think CreateURL needs to receive a dictionary, or you won't be able
to switch or cascade routers easily


On 10/5/07, Ayende Rahien <aye...@ayende.com> wrote:


--
Rafael Teixeira
Castle Stronghold

Hamilton Verissimo

unread,
Oct 5, 2007, 4:13:08 PM10/5/07
to castle-pro...@googlegroups.com
You're a genius!

Reply all
Reply to author
Forward
0 new messages