Search code examples
asp.net-mvc-5attributerouting

MVC 5 AttributeRouting Catch All


How do I create a catch all route with the new Attribute routing in MVC

I tried this: [Route("{pagenode}", Order = 999)]

But when I have a named route like [Route("contact"]

I get the "Multiple controller types were found that match the URL. This can happen if attribute routes on multiple controllers match the requested URL." error.


Solution

  • You can't do this with Attribute routing, do this the MVC4 way:

    Map a route in your routemapper like this:

    routes.MapRoute("RouteName","{*url}",new { controller = "YourFancyController", action = "YourAction" });
    

    This will be your catch-all Route.

    If you would like to map all the routes to their controller you can do this:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
        routes.MapMvcAttributeRoutes();
    
        AreaRegistration.RegisterAllAreas();
    
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
    }