I recently created a site to promote small local coffee shops (LocalCoffee.Rocks). Each coffee shop location has its own profile page. I wanted to make it easy for these shops to point people directly to this dynamic page without having to give them an ugly url containing domain/controller/action/parameterId. I figured out a way to short circut the routing information using the Application_BeginRequest() method in the Global.asax file.
http://localcoffee.rocks/Locations/LocationDetails/4 users can simply enter http://localcoffe.rocks/brewhaha to get to the same profile.
Once I had the value set in the db in the Application_BeginRequest() method to get the “controller” parameter from the url. The trick is that I do have other actual controllers that I want to fire and act just like they are designed in MVC. Thus, after I get the controller parameter I created a list and populated it with all of the names of the real controllers that are in the application. I did this with using reflection by calling the GetTypes() method from the Assembly class. After I had both the passed controller parameter and the list of physical controllers I simply check to see if the passed controller was in the list of physical controllers. If so I did nothing and the site continued to process as normal. If, however, the passed controller was not in the list it most likely is a shortUrl for a shop and so I passed that value to a function to handle that request. Below is the Application_BeginRequest() method.
In the shortUrl processing method I query the database for the passed value which should be located in the ShortUrl column of the database table for each shop location. Then, if a location was found I redirected the browser to the profile page using the full controller/action/parameterId url. If a location was not found the user could have mistyped the short url so I simply redirect them to a UrlNotFound page as seen below.