Named Routes in ASP.NET MVC like Ruby on Rails

Ruby on Rails has named routes with cool helpers that allow you to take advantage of them in your views, etc. By doing something as simple as map.resources :projects, for example, in your routes.rb file:

 

Named Routes Rails

 

you can use these cool named routes in your views to simplify links, etc. in your Rails Applications. Shown below you can see something like new_project_path which is essentially a named route that is automatically generated by the simple map.resources :project mentioned above. As you might expect, there are others generated like projects_path, edit_project_path, etc. You can also get url's by replacing “path“ with “url“, like projects_url.

 

Ruby on Rails

 

So what does this have to do with ASP.NET MVC?

Well, the ASP.NET 2.0 RoadMap discussed Strongly-Type Link Helpers to avoid magic strings to help with compile-time safety, refactoring, etc. The ASP.NET MVC Futures Assembly already has strongly-typed helpers to avoid those magic strings today. However, the helpers use expressions that are rather ugly IMHO, a pain-in-the-butt to type, and apparently have performance issues according to Brad Wilson and reported by Phil Haack.

Interesting enough, David Ebbo has done a little of his code generation magic that he does so well, and came up with a prototype solution using build providers that looks better, but still doesn't seem as cool and intuitive as Rails.

In the end, I think it would be cool to get something similar to the self-documenting and intuitive style of named routes found in Rails, but gives us the compile-time safety and refactoring support. Not sure it is completely possible, but I like the idea of just typing something as simple as new_project_path in an action link helper to generate a link as opposed to the expression builders found in the ASP.NET MVC Futures Assembly.

I guess we'll see.

 

David Hayden

 

Ruby on Rails Posts

 

posted on Sunday, June 14, 2009 1:39 PM

Main

News

Green Tea

.NET Development

Enterprise Library

Patterns & Practices