ASP.Net MVC3路由问题

时间:2012-10-25 19:15:07

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-routing

我正在处理交易网站,并在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
);

直到我查看交易细节时才能正常工作但是当我点击购买按钮购买交易时我收到错误。

在进一步研究中,我发现在点击“购买”按钮时,代码正在调用GetDealsByDealName操作,而不是操作CheckoutDealByDealName。请建议我解决方案。

1 个答案:

答案 0 :(得分: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" }
);

这应该允许细节路线与结账路线不匹配,但是在碰撞期间将首先捕获结账。我真的建议你完全重命名你的路线,但看看你有什么我能理解为什么你想要这些路线。