-1

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

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

当我切换订单但首先是结帐前缀时,它会弄乱“结帐?o =”路线。当我将 Checkout 路线放在第一位时,第二条路线永远不会被击中。

4

1 回答 1

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" }
);
于 2016-04-07T22:00:58.910 回答