我有以下路线:
routes.MapRoute(
name: "One",
url: "admin/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Two",
url: "home/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
有没有办法可以将这些路线合并到一条路线中,它们都指向“Home”控制器?
答案 0 :(得分:1)
尝试
routes.MapRoute(
name: "Default2",
url: "{section}/{id}",
defaults: new {section="home" ,controller = "Home", action = "Index", id = UrlParameter.Optional }
);
适合我
回答评论。 什么问题添加约束?
routes.MapRoute(
name: "Default2",
url: "{section}/{id}",
defaults: new { section = "home", controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints: new { section = "admin|home" }
);