3

I have three specific routes:

    routes.MapRoute(
       "Home Page",
       "", 
       new { controller = "Home", action = "Index" } 
       );

    routes.MapRoute(
       "Admin Section",
       "AdminSection/{action}/{id}",
       new { controller = "AdminSection", action = "Index", id = UrlParameter.Optional }
       );

    routes.MapRoute(
       "Listings",
       "{controller}/{action}/{id}",
       new { controller = "Listings", action = "Index", id = UrlParameter.Optional }
       );

Basically, the first two routes work as planned, however, I want everything that isn't specifically in a route to be redirected to the listings controller.

I am still quite new to routing and have been trying to Google this for the past hour without any luck - I know exactly what is going on here, but, I don't know how to fix it.

I have used RouteDebugger, and I can see that it is hitting the route, but, the issue is that it will only go to the Listings controller if a controller is not specified - but, obviously there will always be something there.

I have tried a few different combinations - I thought I was on to a winner by removing the {controller} part of the URL and still defining the default value, but, I am not having much luck.

Does anyone know what I need to do?

Wil
  • 10,234
  • 12
  • 54
  • 81

3 Answers3

4

How about this:

routes.MapRoute("Listings", "{action}/{id}", 
        new { controller = "Listings", action = "Index", id = UrlParameter.Optional });

site.com/test :

It'll go to action: test, controller: listing, id = blank

enter image description here

mnsr
  • 12,337
  • 4
  • 53
  • 79
  • Once again, I feel like an idiot - This is exactly what I was trying to do, but, I made a mistake in the `new {...}` part which I didn't see until I copied your example in to it. Thank you so much. – Wil Sep 23 '11 at 05:15
3

Edit: As I understand it you want a catch-all route.

http://richarddingwall.name/2008/08/09/three-common-aspnet-mvc-url-routing-issues/

routes.MapRoute("Listings", "{*url}",
    new { controller = "Listings", action = "Index" }
);

Original:

I can't test this at the moment but

routes.MapRoute(
   "Listings",
   "{anythingOtherThanController}/{action}/{id}",
   new { controller = "Listings", action = "Index", id = UrlParameter.Optional }
   );

This should work.

In your Listings controller, just accept a string parameter "anythingOtherThanController" and it will get bound to it.

The main problem here is that /some/action will be mapped to the same action as /another/action. So I'm not sure what you're trying to do here :)

Daryl Teo
  • 5,394
  • 1
  • 31
  • 37
  • +1 for all your help and I will read that (interesting) link when I have a bit more time... However, I accepted an answer as I tried it and it does exactly what I want. – Wil Sep 23 '11 at 05:21
  • No probs. As long as it fixes your problem. Sorry I couldn't understand your requirements :) Do test though, just in case. – Daryl Teo Sep 23 '11 at 05:22
1

Provide a default route and provide controller name as listings controller. Keep this route mapping at the bottom of all the mappings.

 routes.MapRoute(
   "Default",
   "{controller}/{action}/{id}",
   new { controller = "Listings", action = "Index", id = UrlParameter.Optional }
   );

Sorry I got sequence mixed.

kaps
  • 468
  • 4
  • 18
  • This does not work - I have already tried it... If I want a url of "http://site.com/bla" - this will try to load the bla controller with the default action of index .... I need it to load the bla action on the default controller of listings. (When I say try it, I mean lower down in priorities... if it was first, it would ruin the other routes). – Wil Sep 23 '11 at 04:55
  • This will capture everything, including AdminSection, wouldn't it? – Daryl Teo Sep 23 '11 at 04:55
  • I'd suspect if you did this, and someone typed in adminsection/whatever, it won't go to the adminsection route, instead this default route will try and execute it. – mnsr Sep 23 '11 at 04:56
  • Ohh I was confused for a moment about place (top/bottom). I used it earlier on one of my project. I have updated the answer. – kaps Sep 23 '11 at 05:09