How to Create a Catch All Requests Route in ASP.NET MVC 5

I will soon move a web application to a different domain. Without any extra work this change will break the existing URLs. To prevent this from happening, I try a new approach with a small replacement application that accepts every request to the old domain name, logs its referring site and redirects the user after a few seconds to the new application.

The biggest challenge for this endeavour is to create a catch-all route configuration. You can replace all existing rules of the ASP.NET MVC 5 Template in the class RouteConfig with one that uses “{*url}” as the URL pattern:

This rule will now redirect all requests to the Index() method of the EverythingController class. In this method we can log all the data we get from the Request object using the logging framework of our choice:

In the view partial I can show a short explanation about the new URL and then do a redirect using JavaScript:

With these three parts I can prevent the dreaded 404 errors without investing too much effort in keeping up the old structure. I think these little app will make the progression to the new domain a lot smoother.

The info about the new domain can be seen

3 thoughts on “How to Create a Catch All Requests Route in ASP.NET MVC 5”

  1. Thank you for sharing.
    Adding [Route(“{*url}”)] attribute to your action would save you the RouteConfig component 😉

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.