In my mind one of the most abused bits of functionality in ASP.net MVC is the routing engine. I donâ€™t think this is a problem unique to ASP.net MVC as other MVC frameworks like Rails also make heavy use of routing.
In a nutshell routing allows you to map the URL(or URI if you want to beÂ entirelyÂ correct) to a file or action on the server. For instance if you look at the URL for this post
You can see that there is some information buit into it. I donâ€™t know the internals of wordpress but you can be pretty much assured that there isnâ€™t actually a file on disk calledÂ why-you-shouldnt-be-bothering-with-routes in the directoryÂ /2013/03/04/. Instead this URL is used by a routing engine to passÂ appropriateÂ parameters to a script which looks up information in a database. The combination of the domain and the name of the blog is probably sufficient to identify the record in the database. The date information in there is just a hint to readers so they know when the blog was written.
The only thing is: its wrong. Iâ€™m writing this blog on the 9th of March and it is scheduled to be published on the 13th. The reason that date is there is that I created a draft on the 4th. That I created a draft on the 4th isÂ immaterial. I have some drafts from 9 months ago. Heck, I have a half written rant about an architectural failure at Backblaze which is so out of date that it will likely never be published. The draft date is not important in the least.Â FortunatelyÂ nobody looks at the URL to gather this information.
URLs are not meant to convey useful information to people, theyâ€™re there as instructions to computers. From time to time it is useful to have a friendly URL that people can remember but this is typically only for allowing them to type it in from a piece of paper. URLÂ shorteningÂ services are fantastic for that.
Embedding information in the URL might look nice but it has very limited utility. In the .net world routing is provided by System.Web.Routing and this is typically configured in the startup of an MVC application. I have seen a number of MVC applications which have dozens, even hundreds of routes defined. Even Phil Haacked has contributed to the madness by providing a route debugger. Stop the madness! Use the default routes!
Having complex routes makes it very difficult to figure out which controller it is that is causing you trouble. It is also a boat load of extra code you need to test and understand. The fact that a route debugger is necessary should be a big hint that your logic is too complicated. The default route is sufficient for 99% of the requests coming to your site.
The only legitimate uses I can think of for routing requests are:
You need to do some optimization for search engines. Apparently google place some emphasis on the structure of a URL. It is difficult to tell because google keep pretty quiet about how they do page ranking. Optimizing for search engines is a prettyÂ sleazyÂ business anyway so youâ€™re likely better of not even trying.
Mapping old URLs. This is a really good call. It sucks when there is a change to the underlying engine behind a website and now none of your links work. By setting up a mapping you can avoid a lot of 404s in your error logs.
If these two reasons donâ€™t match your use case then donâ€™t even bother adding custom routes. It will save you headaches in the future.