ASP.NET MVC - MapRoutes for data driven views

by 25. September 2010 23:38

The Community Starter Kit (CSK) is a data driven application; there are no actual web pages.   When I upgraded the CSK to support ASP.NET Beta 2 (Microsoft abandoned support of the CSK) I renamed it the Community Advanced Starter Kit (CASK) because it was off the beaten path and required advanced skills to understand (learning curve was steep).

CASK seems to have a following and is a perfect tool to help me learn/master the ASP.NET MVC environment; so I'll use this opportunity to upgrade CASK to version 3.0.  I am finding that ASP.NET MVC is lending itself to the "data driven" environment that CASK thrives in.  

The first thing I have to do is configure the routes so that ASP.NET MVC does not complain if the requested view does not exist.  The following are the behaviors I was looking for:

  1. It had to support default behavior.   I don't want CASK V3 to be far off the beaten path (like CSK was to ASP.NET developers); I'd like people learning (or knowledgable) of ASP.NET MVC to be able to quickly ramp up.   It also will have great value for the functionality that will have actual pages such as the admin section.
  2. I needed it to support simple addresses, e.g., http://mydomain/1?hello=world
  3. I needed it to support the dynamic address generated by CASK, e.g., http://mydomain/Main/Section/Detail

The following meets these requirements (documented in detail after the image):

Getting my mind around MapRoute was tricky because there wasn't a lot of documentation on the subject.  Fortunately Scott Hanselman has great webcast on the subject and with his Nerd Dinner demo I was able to figure out how to configure my routes.  I used Phil Haacks route debugger to work out the details as it is not straight forward as one would think; at least not with available documentation.

To keep the default behavior, and get the other behaviors I required, I first had to remove the "id" from the default MapRoute (line 35 above).   With that change the default pages, to include login, continued to work while opening up three segments to CASK, e.g.,   This took care of my first requirement, now the HomeController will intercept any addresses that have two segments, e.g. mydomain/Home/Main which is acceptable for the purposes of CASK (it's addresses will have more segments OR contain a single segment).

Next I needed all other segments counts to be handled by the CaskController so that it can dynamically load the applicable content from the database.  I found I was able to successfully achive this for any segment count > 2 using the CaskAll MapRoute on line 40.   Note the "*caskAll" has an asterisk, I got this from Scott's Nerd Dinner sample and found that without this wildcard, the behavior I was seeking was not there (couldn't find documentation on the asterisk but it worked!).

With the noted addition I was surprised that it didn't pick up the single segment, e.g, mydomain/1?hello=world.   I couldn't move CaskAll before the default MapRoute or I would lose the default behavior so I added the CaskId MapRoute on line 27 and all worked as required.

The following is the controller code and debug information when this address
http://localhost:16311/1?Hello=World  is typed in:


Blog videos and references to CodePlex projects are no longer valid