Advanced Routing

As mentioned at the end of Chapter 9, routing is simple to learn yet challenging to master. Here we describe a few advanced tips Phil recommends to simplify some otherwise tricky routing scenarios.

RouteMagic

In Chapter 9, we mentioned the RouteMagic project, which is an open source project available on CodePlex at http://routemagic.codeplex.com/.

Install-Package RouteMagic.Mvc

This project is also available as a NuGet package appropriately named RouteMagic. RouteMagic is a pet project of Phil Haack, one of the authors of this book, and provides useful extensions to ASP.NET Routing that go above and beyond what's included “in the box.”

One useful extension included in the RouteMagic package is support for redirect routes. As noted usability expert Jakob Nielsen has recommended, “persistent URLs don't change,” and redirect routes will help you support that.

One of the benefits of routing is that you can change your URL structure all you want during development by manipulating your routes. When you do so, all the URLs in your site are updated automatically to be correct, which is a nice feature. But once you deploy your site to the public, this feature becomes a detriment, as others start to link to the URLs you've deployed. You don't want to change a route at this point and break every incoming URL.

Unless…you properly redirect. After installing RouteMagic, you'll be able to write redirect routes which take in an old route and redirect it to a new route, as follows: ...

Get Professional ASP.NET MVC 4 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.