CommunityBlog: Routing Design Phase 1

Monday, September 08, 2008 9:09 AM

Routing is an integral part of Microsoft .NET Framework 3.5 SP1. In ASP.NET MVC application it plays an important role in mapping URL to controllers and actions. We define the mapping in routing table, and then the routing mechanism will read this table to determine the Controller and Action that will handle the incoming request.

Here is the routing design for CommunityBlog in current phase:

NoURL PatternController / Action
1 blog/{postAlias} Controller = Blog
Action = ShowSinglePost
2 blog.aspx Controller = Blog
Action = Index
3 blog/p/{pageNo} Controller = Blog
Action = Index
4 blog/{blogAlias}/default.aspx Controller = Blog
Action = Index
5 blog/{blogAlias}/{postAlias} Controller = Blog
Action = ShowSinglePost
6 blog/{blogAlias}/p/{pageNo} Controller = Blog
Action = Index
7 sitemap.xml.aspx Controller = Sitemap
Action = Index
8 [blank] Controller = Blog
Action = Index

With the above routing definition, CommunityBlog should be able to handle the following sample URLs:

  • http://localhost/blog/lorem-ipsum.aspx
  • http://localhost/blog.aspx
  • http://localhost/blog/p/2.aspx
  • http://localhost/blog/other/default.aspx
  • http://localhost/blog/other/lorem-ipsum.aspx
  • http://localhost/blog/other/p/2.aspx
  • http://localhost/sitemap.xml.aspx
  • http://localhost/

If the URL does not specify blogAlias, the system will use the default blogAlias stored in application configuration file. In a post-list page, including current home page, if pageNo parameter not specified, the default pageNo to use is 1.

I still handle .aspx extension in routing table because for now I plan to deploy CommunityBlog in IIS 6. As you might already knew, IIS 6 can't handle extension-less URL naturally. You have to perform a workaround to enable IIS 6 handles extension-less URL. I know this is subject to change later when IIS 7 is widely available. So, it's important to have routing table initialization highly configurable.

As a consequence of the inability to handle extension-less URL, routing entry no 8 is also useless. IIS will handle the request, and then redirect the request to default page. This redirected request won't be handled by ASP.NET routing module. Instead, it will be handled directly by the default page. This behavior is also subject to change when IIS 7 is widely available.

As conclusion, routing is the new mechanism from Microsoft .NET Framework 3.5 SP1 that will map URLs into their corresponding controllers and actions. Routing is defined in a routing table configured in global application's start event. Here, I define eight route entries for CommunityBlog.