简体   繁体   中英

Routing same name controllers and different areas

I have 2 same name controllers, ShopController , one is a normal controller (basically where you usually put them), and the other one is in an area called Admin .

Here are my routes:

routes.MapRoute("Shop", "shop/{action}/{id}", new { controller = "Shop", action = "Index", id = UrlParameter.Optional }, new[] { "CMS.Controllers" });
routes.MapRoute("CategoriesrPartial", "Admin/Shop/CategoriesNavPartial", new { controller = "Shop", action = "CategoriesNavPartial" }, new[] { "CMS.Areas.Admin.Controllers" });

The bottom is supposed to be connected to the Admin area one, and the top one to the regular one.

I tried adding namespaces based on another answer here on SO but they don't make a difference, even if I put some gibberish in it does not make a difference.

How can I have these routes refer to exact controllers I want?

the order when you add routes to the route collection is significant, the engine will look into that route collection and the first that will match that route will be picked as the winner.

if your first route matches, means that it start with the word shop then it will hit the controller Shop, if not it will continue matching the structure over the RouteData collection. There is no need to add the namespaces on this case

shop/{action}  -> should start with shop word then you need to specify the action in your anonymous object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM