3

我正在一个交易网站上工作,并在 global.asax 中定义了以下路线。

routes.MapRoute(
    "AboutFooter",
    "about-bicker-shop",
    new { controller = "Footer", action = "About" }
);

routes.MapRoute(
    "ContactFooter",
    "contact-bickershop",
    new { controller = "Footer", action = "Contact" }
);

routes.MapRoute(
    "PrivacyPolicyFooter",
    "privacy-policy",
    new { controller = "Footer", action = "PrivacyPolicy" }
);

routes.MapRoute(
    "TermsAndConditionsFooter",
    "terms-and-conditions",
    new { controller = "Footer", action = "TermsAndConditions" }
);

routes.MapRoute(
    "SiteMapFooter",
    "sitemap",
    new { controller = "Footer", action = "SiteMap" }
);

routes.MapRoute(
    "FAQFooter",
    "faq",
    new { controller = "Footer", action = "FAQ" }
);

routes.MapRoute(
    "UnsubscribeFooter",
    "unsubscribe",
    new { controller = "Footer", action = "Unsubscribe" }
);

routes.MapRoute(
    "GetDealsByCity",
    "daily-bickers/{cityName}",
    new { controller = "Home", action = "Home" }
);

routes.MapRoute(
    "GetDealsbyCategory",
    "daily-bickers/{cityname}/{category}",
    new { controller = "Home", action = "GetDealsByCategory" }
);

routes.MapRoute(
    "GetDealDetails",
    "{cityName}/{dealName}",
    new { controller = "Home", action = "GetDealsByDealName" }
);

routes.MapRoute(
    "DealCheckout",
    "{cityName}/{dealName}/checkout",
    new { controller = "Home", action = "CheckoutDealByDealName" }
);

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

在我查看交易详情之前它工作正常,但是当我点击购买按钮购买交易时出现错误。

在进一步的研究中,我发现点击购买按钮时,代码调用的是GetDealsByDealNameaction 而不是 action CheckoutDealByDealName。请建议我解决方案。

4

1 回答 1

2

MVC 将始终提供它可以在路由表中找到的与请求的路由匹配的第一个路由。在这种情况下,您获得错误路线的原因是因为它们都与GetDealDetails路线匹配。即使第二条路线更具体,它也永远不会到达,因为它GetDealDetails总是首先匹配。尝试像这样重新排序您的路线:

routes.MapRoute(
    "DealCheckout",
    "{cityName}/{dealName}/checkout",
    new { controller = "Home", action = "CheckoutDealByDealName" }
);

routes.MapRoute(
    "GetDealDetails",
    "{cityName}/{dealName}",
    new { controller = "Home", action = "GetDealsByDealName" }
);

这应该允许 details 路线与 checkout 路线不匹配,但在冲突期间将首先捕获 checkout。我真的建议你完全重命名你的路线,但是看看你有什么我可以理解你为什么想要这些路线。

于 2012-10-25T19:46:25.750 回答