I\'m developing a new application and I\'m trying to come up with a convention f
ID: 651939 • Letter: I
Question
I'm developing a new application and I'm trying to come up with a convention for defining routes. Ideally, I would like it to be simple, flexible, and reversible.
Here are some from popular frameworks:
Backbone
route matches
-----------------------------------------------------
help help
search/:query search/kiwis
search/:query/p:page search/kiwis/p7
file/*path file/nested/folder/file.txt
docs/:section(/:subsection) docs/faq, docs/faq/installing
Django
Uses regexes, but comes with a complex regex reverser
route matches
-------------------------------------------------------------------------------------
^articles/2003/$ /articles/2003/
^articles/(d{4})/(d{2})/$ /articles/2005/03/
^articles/(?P<year>d{4})/(?P<month>d{2})/(?P<day>d{2})/$ /articles/2003/03/03/
Ruby on Rails
:controller/:action/:id/with_user/:user_id
books/*section/:title
ASP.NET / MVC 5
{controller}/{action}/{id} /Products/show/beverages
{resource}.axd/{*pathInfo} /WebResource.axd?d=...
{productId:int}/{productTitle} /5/asp-net-mvc
books/{isbn?} /books, /books/1430210079
Django's is probably the most flexible, and it's already familiar to people that know regexes, but it's also the most difficult to read and reverse.
Backbone, RoR and ASP.NET all allow optional and wildcard/splat parts, with various syntaxes. MVC 5 has some options for restricting the parameters inline ala :int.
Where might these schemes fall short, and how could they be fixed?
Explanation / Answer
I've used ASP.NET MVC for years. Lately I've been doing research on Rails and Django to see if they have something more to offer. I've done reading the routing part of their documentation, but I haven't done any real apps yet, so I'm not an expert. I have no experience with Backbone, so I'll leave that out.
In short, Rails has the most powerful routing system of the three, Django has the weakest and ASP.NET MVC is somewhere between.
All have:
- named routes
- wildcards (in ASP.NET MVC only the last parameter)
- extra data (pass some values that are not part of the route)
- regex constraints
- helpers to create urls
- some kind of route prefix (namespace/area)
Ruby on Rails and ASP.NET MVC have:
- generic routing (one route, multiple controllers/actions)
- default values
- code constraints
Ruby on Rails has:
- resource routing
- powerful redirection
Example of regex constraint in Ruby on Rails (id has to be a number)
get ':controller/:id/:action', constraints: {id: /^d+/}
Example of code constraint in ASP.NET MVC (check that {year}/{month}/{day} forms a valid date)
routes.MapRoute("date", "{year}/{month}/{day}",
new { controller = "Test", action = "Date" },
new { year = @"d{4}", month = @"d{2}", day = @"d{2}", date = new DateConstraint() });
// -----
public class DateConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
try
{
var year = int.Parse(values["year"] as string);
var month = int.Parse(values["month"] as string);
var day = int.Parse(values["day"] as string);
var date = new DateTime(year, month, day);
return true;
}
catch
{
return false;
}
}
}
Example of redirection in Ruby on Rails
get '/stories/:name', to: redirect {|params, req| "/posts/#{params[:name].pluralize}" }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.