用于结帐的MVC4路由子文件夹目录

时间:2016-04-07 20:28:31

标签: asp.net-mvc asp.net-mvc-4 url-routing asp.net-mvc-routing routeconfig

我需要做些什么才能将这些网址映射到我的路线?

example.com/checkout?o=1234
example.com/checkout/shipping?o=1234
example.com/checkout/payment?o=1234
example.com/checkout/review?o=1234
example.com/checkout/receipt?o=1234

在RouteConfig中,我已经定义了这些:

 routes.MapRoute(
         name: "Checkout",
         url: "checkout",
         defaults: new { controller = "Checkout", action = "Index", o = UrlParameter.Optional }
     );

 routes.MapRoute(
          name: "checkout Prefix",
          url: "Checkout/{controller}/{action}/{o}",
          defaults: new { controller = "Shipping|Payment|Review|Receipt", action = "Index", o = UrlParameter.Optional }
          );

控制器文件夹是:

 Controllers
      CheckoutController.cs
            Checkout
                 ShippingController.cs
                 PaymentController.cs
                 ReviewController.cs
                 ReceiptController.cs

当我首先切换订单而不是Checkout Prefix时,它会弄乱" checkout?o ="路线。当我把Checkout路线放在第一位时,第二条路线永远不会被击中。

1 个答案:

答案 0 :(得分:1)

您假设o是路线的一部分是不正确的。查询字符串不会作为匹配的一部分进行评估。

此外,您应该在约束中指定正则表达式,而不是默认值。

routes.MapRoute(
    name: "Checkout",
    url: "checkout",
    defaults: new { controller = "Checkout", action = "Index" }
);

routes.MapRoute(
    name: "checkout Prefix",
    url: "checkout/{controller}",
    defaults: new { action = "Index" },
    constraints: new { controller = "Shipping|Payment|Review|Receipt" }
);