2012-10-25 114 views
3

我正在處理交易網站,並在global.asax中定義了以下路由。ASP.Net MVC3路由問題

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

需要查看生成鏈接的方式,以瞭解爲什麼它們可能無法正確路由。 – mckeejm

+0

您可以將標記貼上您正在使用的操作鏈接或按鈕嗎? – AntLaC

+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" } 
); 

這應該允許細節路線不符合結賬路線,但結賬會先在碰撞過程中被抓。我真的建議你完全重新命名你的路線,但看看你有什麼我可以理解你爲什麼要這些路線。

+0

如上所述,我嘗試重新排序路線,但沒有成功。仍然得到相同的錯誤。 –

+0

我嘗試將路由重命名爲routes.MapRoute( 「DealCheckout」, 「{cityName}/{dealName}/checkout」, new {controller =「Home」,action =「CheckoutDealByDealName」} );但這並沒有解決。看來我沒有別的選擇,只能徹底改變路線。 –

+0

通過更改路由到'checkout/{cityName}/{dealName}',您可能會獲得更多成功。這將改變路線足夠獨特,但它仍然看起來非常接近你原來的意圖。 –