3

I am working on a deal website and have defined the following route in the 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
);

It works fine till the time I am viewing the details of the deal but I am getting an error when I click on the buy button to buy the deal.

On further research I found that on clicking the buy button, the code is invoking the GetDealsByDealName action and not the action CheckoutDealByDealName. Please suggest me the solution.

4
  • 1
    need to see the way the links are generated to understand why they may not be routing appropriately. Commented Oct 25, 2012 at 19:16
  • Can you post the mark up for the action links or buttons you are using? Commented Oct 25, 2012 at 19:17
  • <a href="@Url.Action("CheckoutDealByDealName", " Home", new { cityName = ViewBag.CityName, dealName = Model.GetDealDetailsByDealId.First().DealName.ToSeoUrl() })"> <img src="../../Content/Images/buy_now2.png" alt="" /> </a> Commented Oct 25, 2012 at 19:29
  • The following link is generated - http://{url}/{controller}/{action}?cityname="{cityname}anddealname="{dealname}/checkout while my expectation is http://{url}/{cityname}/{dealname}/checkout Commented Oct 26, 2012 at 8:54

1 Answer 1

2

MVC will always provide the very first route that it can find in the routing table that matches the route requested. In this case, the reason you're getting the wrong route is because they both match to the GetDealDetails route. Even though the second route is more specific, it won't ever be reached because the GetDealDetails will always match first. Try re-ordering your routes like this:

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

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

This should allow the details route to not match the checkout route, but the checkout will be caught first during a collision. I'd really suggest you rename your routes entirely, but looking at what you have I can understand why you want these routes.

Sign up to request clarification or add additional context in comments.

4 Comments

As suggested, I tried re-ordering the routes but no success. Still getting the same error.
I tried renaming the route as routes.MapRoute( "DealCheckout", "{cityName}/{dealName}/checkout", new { controller = "Home", action = "CheckoutDealByDealName" } ); but this did not worked out too. It seems I have no other option but to completely change the route.
You might have more success by changing the route to checkout/{cityName}/{dealName}. That would change the route enough to be unique, but it'd still look pretty close to your original intent.
Even tried the same route but its not working, may be reason being both the actions has same signature and may be mvc routing considers the action signature and not the complete url to be formed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.