Search code examples
asp.net-mvcasp.net-mvc-3asp.net-routing

ASP.NET MVC: How to force all links to goto 1 page?


I am trying to figure out how i can create a route configuration so that all URLS will goto the same VIEW (Page).

CUrrently of course if i do for example

/Products/Id

Then this would look in the Products controller.

I would like to always goto my MainController and the same action no matter what the URL is

Is this possible?

Thanks in advance


Solution

  • This is possible to be done with a catchAll route:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
        routes.MapRoute(
            "CatchAll",
            "{*url}",
            new { controller = "Main", action = "Index" }
        );
    }
    

    Alternatively you could have your default route and put the catchAll route after it so that if no other route is matched, the catchAll route will get it