Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
Hi. I have a problem with rouing in asp.net mvc.
How can i redirect :

route - controller/action
{brand}-car-prices -> /PriceList/PricesByBrand (For example : "toyota-car-prices")
{color}-{brand}-car-prices -> /PriceList/PricesByColorAndBrand {for example : "green-toyota-car-prices"}
{modelyear}-car-prices -> /PriceList/PricesByModelYear {for example "2005-car-prices"}
{modelyear}-{brand}-car-prices -> /PriceList/PricesByModelYearAndBrand {for example "2005-toyota-car-prices"}

when i routing, all routes redirecting same action (page).
How can i do correct?

What I have tried:

C#
routes.MapRoute(
              name: "carprices",
               url: "{brand}-car-prices/{page}",
              defaults: new { controller = "PriceList", action = "PricesByBrand ", brand= UrlParameter.Optional, page = UrlParameter.Optional },
              namespaces: new[] { "MyProject.Controllers" }
          );

            routes.MapRoute(
              name: "colorcarprices",
               url: "{color}-{brand}-car-prices/{page}",
              defaults: new { controller = "PriceList", action = "PricesByColorAndBrand ", color= UrlParameter.Optional, brand= UrlParameter.Optional, page = UrlParameter.Optional },
              namespaces: new[] { "MyProject.Controllers" }
          );
Posted
Updated 5-Jul-18 20:27pm
Comments
MadMyche 5-Jul-18 13:20pm    
Just wait till you try passing in "Daimler-Benz" as a brand

You have some issue in routes



Quote:
route - controller/action
{brand}-car-prices -> /PriceList/PricesByBrand (For example : "toyota-car-prices")
{color}-{brand}-car-prices -> /PriceList/PricesByColorAndBrand {for example : "green-toyota-car-prices"}
{modelyear}-car-prices -> /PriceList/PricesByModelYear {for example "2005-car-prices"}
{modelyear}-{brand}-car-prices -> /PriceList/PricesByModelYearAndBrand {for example "2005-toyota-car-prices"}



Here take 2 routes {brand}-car-prices and {modelyear}-car-prices .
They are of same structure there is no way to separate them.

Same issue is with the {color}-{brand}-car-prices and {modelyear}-{brand}-car-prices

The order of rule is also important

when the rule {brand}-car-prices come first it match any other route you are providing. So it is executed.

You can check these links also

Route value with dashes[^]

MVC routing problem - trying to pick up the remainder after a hyphen in the route[^]
 
Share this answer
 
v5
The order of routing rules is matter...
The look for the rule will stop on the first fit...
In most cases that means that longer rules (with the same opening) should come before the short one...
So switch between your two rules...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900