如何使用不同路径名开始的两个MVC路由在MVC4中使用相同的控制器

时间:2013-06-14 11:16:22

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 routes

我有以下路线:

   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”控制器?

1 个答案:

答案 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" }
       );