ASP.NET MVC 3 Routes - Allow Arbitrary Number of Slashes With Catch All

I recently had issues where Google was seeing misspellings of a domain names as different websites themselves, and possibly as duplicate content.  The domains were bound to the same web application in IIS and I wanted to fix this without editing the source of the web application itself.

I had a few options, one of which is to use Cantonal URLs, but that would involve editing the web app to some degree.  So before going that way, I wanted to start making permanent redirects to the correct website URL.  The quickest approach I could think of was to create a global redirect as a new web application and reassign all the domain name bindings needed.  So I created a simple MVC 3 Web Application called GlobalPermanentRedirect which effectively takes any page and redirects the path / query to domain specified in the appSettings.

The code is very simple, however I got stumped for a few minutes with Routing, as I forgot about the catch-all parameter using *.  Here's the code, it's really only about three lines.

In the routing area (Global.ascx) register the route:

   "Default", // Route name
   "{*path}", // catch all urls
   new { controller = "Redirect", action = "Index" } //defaults

Create a controller called RedirectController.cs with the following line of code:

public ActionResult Index()  
    return RedirectPermanent(ConfigurationManager.AppSettings["BaseUrl"] + Request.Url.PathAndQuery.ToString());

In web.Config under appSettings:

<add value="" key="BaseUrl" />