How do I redirect everything to a single controller?
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.Opti开发者_开发问答onal }
);
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?
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
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 :)
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.
精彩评论