如何在MVC 5中设置多个路由到同一个动作方法?

时间:2017-01-28 11:07:36

标签: asp.net-mvc angular

我正在使用MVC 5和Angular 2进行单页面应用。

我的要求是我需要设置多个路由到同一控制器/动作方法。

如果用户输入url http://localhost:xxxx/home/index,它将定位“home”控制器的“index”操作方法。

我也是 希望“http://localhost:xxxx/products”和“http://localhost:xxxx/categories”指向“家庭”控制器的“索引”操作方法。

注意:“产品”和“类别”都是更好的控制器或操作方法。

1 个答案:

答案 0 :(得分:3)

添加以下2条路线完成了这项工作。

      routes.MapRoute(
          name: "categories",
          url: "categories",
          defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
      );

        routes.MapRoute(
          name: "products",
          url: "products",
          defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
      );


       routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );