0

Possible Duplicate:
ASP.NET MVC - Catch All Route And Default Route

routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

        routes.MapRoute(
            name: "CatchAll",
            url: "{id}",
            defaults: new { controller = "Home", action = "CatchAll", id = UrlParameter.Optional }
        );

Is there an easy way of implemeting the following routing pattern. Basically i would like the catch all to kick in if the controller does not exist. The pages are dynamically populated for these pages if they exist in the database. Other wise i'll throw an error. I do NOT want a route that starts with any thing else. for example

routes.MapRoute(
        name: "CatchAll",
        url: "Caught/{id}",
        defaults: new { controller = "Home", action = "CatchAll", id = UrlParameter.Optional }
    );

I can get it so that each work individually but to get them to work at the same time is proving a little difficult. I'm guessing i will need to overload something somewhere. There must be a guru out there who knows the answer! :D

Community
  • 1
  • 1
Adween
  • 2,792
  • 2
  • 18
  • 20
  • 2
    Similar question that might help you: http://stackoverflow.com/questions/4001081/asp-net-mvc-catch-all-route-and-default-route – Niklas Oct 02 '12 at 14:19
  • thankyou @Zalk! i did look for the answer, honest! Bang on though thanks, constraits it is! – Adween Oct 02 '12 at 14:57
  • if you post your solution @zalk i'll mark you as the correct answer. – Adween Oct 03 '12 at 14:01
  • That's alright, It's too late now since it's been closed. But I'm glad you solved it =) – Niklas Oct 03 '12 at 19:08

1 Answers1

0

These two routes are effectively the same thing as far as matching is concerned. The Default route is really a catch all that directs the user to Home.Index. If the controller or action does not exist a 404 error will be raised. You could redirect 404s to a specific controller in the web config.

This might be helpful: Returning 404 Error ASP.NET MVC 3

Community
  • 1
  • 1
Doug Mitchell
  • 192
  • 1
  • 9
  • @Zalk got the answer right, it can be done using constraints, I pass in the controllers that exist and boom both instances now work. – Adween Oct 03 '12 at 14:00