Header background image for AlwaysMoveForward.com
Software

My MVC Experiment Part 8 - Route Mapping

By Arthur Correa • Author

So I've finally gotten to start working on actually MVC portions of the Blog code.  Lets see how does this stuff work?  Hmm ASP.Net looks at the url coming in and determines the controller and action from the url pretty straightforward.

So just as an example here I am at http://www.alwaysmoveforward.com/software/Blog/Post/2009/2/19/My_MVC_Experiment_Part_8_-_Route_Mapping.  How does MVC use this url?

Well it starts off by ignoring the first part of the url that contains the site. 

 

Then, by default it looks next for the controller portion of the URL. 

It takes that portion of the Url (in this case it says Blog, and looks for a Controller class called BlogController.  ASP.Net will then instantiate an instance of that Controller and try to do something with it.

But what does it do?  Well it has to figure out what Action to execute on the BlogController class that it created in the last step.  To do that it looks again at the URL, where a portion is reserved for the target controller Action.

Hmm so according to the URL the Post Action will be executed.  This translates into a method on the BlogController called Post.  This method will do the work neccessary to get the right data into the Model.  Once that data is in the Model the Post View is rendered automatically.

So what is up with this chunk in the middle here, the software piece? 

Well that is some custom mapping that I did on my own.  Once I decided that I was going to have a multi blog site I realized I didn't want my url to be

http://www.alwaysmoveforward.com/software/Blog/index.  I could have done that very easily with some simple Map Routing that ASP.Net already supports, but syntactically I didn't like it at all.  I wanted my blogs to appear to be different subfolders with their own data, rather than being just another id value in the url.

So to that end I prepended the route with the Target Blog identifier.  So it comes before the Controller.  The upside to this is that I get pretty URLs.  The downside is that there are situations where default relative Urls can't be generated and I have to build them by hand. 

What are these situations?  Well the home page for 1.  When you first hit http://www.alwaysmoveforward.com, you don't have a target blog in your url, so having just a single default route of {targetBlog} /{controller}/{action} configured in my blog code would not work for everything.

Instead I had to leave the Default route, with just {controller}/{action}, that is created for you automatically, and then also have a route that configured with the TargetBlog.

Now, this solution is all very straightforward, no problem.  However a lot of trial and error went into this final solution.  I tried a number of things such as

  1. Just getting the target blog out of the request and not having it as a map route (essentially it was nothing more than a section of the URL I had to pull out by hand).  This did work, it just wasn't as clean as having a mapped route automatically populate an Action parameter.
  2. Having explicit routes targetblogs called out for explicit controller/action combinations.  For example have {targetBlog}/Blog/Post/{id} and {targetBlog}/Blog/Index.

Wait a second, #2 seems pretty complex, but it defintiely does work.  So why did I try it that way?  Well I also have pages that are not blog specific.  Pages related to User management, site configuration, and user preferences.  I initially tried to just use the Default route of {controller}/{action} and add onto that for the blog specific routes.

That quickly turned into the crazy situation of option 2, which is when I changed to, as I said before, having a Default route with just the {coontroller}/{action}.  As well as a route that has {targetBlog}/{controller}/{action}.    This works, its pretty clean, not a lot of extra code, and makes for a nice looking URL to map my posts to.