I submitted a patch to the MvcContrib project last week containing my fluent interface for route definitions (the one I blogged about last week). It was applied yesterday (revision 541). When I prepared the patch I did some minor changes, for example renamed the route from SagaRoute to MvcRoute (Saga is related to the name of the app I am building).

Some examples:

  .ToDefaultAction<HomeController>(x => x.Index(), new {id="0"})
  .AddWithName("Default", routes);

  .ToDefaultAction<QuestionsController>(x => x.ViewByTag(""))
  .AddWithName("QuestionsByTag", routes);
  .WithConstraints(new { id="^[0-9]+$" })
  .ToDefaultAction<QuestionsController>(x => x.ViewQuestion(0))
  .AddWithName("QuestionsById", routes);
  .ToDefaultAction<UserController>(x => x.ViewUser(""))
  .AddWithName("UserByName", routes);

I added a WithConstraints method. If you do not like the lambda approach but still like the fluent API you can use the WithDefaults method that takes in an anonymous type (like the default MapRoute method do).


Anonymous said...

Shouldn't it be "Map" instead of "Mapp".

Torkel Ödegaard said...

Yes, it should :)

I will try to get it corrected..

Andrew Allison said...

Nice post thanks saved my sanity while trying to use MVCContrib ;)