Reputation: 89
My website uses categories and sub-categories.
I'd like the follow mapping:
/Category/Fruit
/Category/Fruit/Apples
But if I use the below:
routes.MapRoute(
"Category", // Route name
"Category/{category}/{subcategory}", // URL with parameters
new { controller = "Entity", action = "Category" } // Parameter defaults
);
I get a 404 for /Category/Fruit however /Category/Fruit/Apples works ok. I'd like /Category/Fruit to work as well but I can't add another route with the same name. How do I get around this?
Thanks in advance.
Upvotes: 1
Views: 80
Reputation: 4886
This will work for your scenario
routes.MapRoute(
"Default", // Route name
"Category/{category}/{subcategory}/{id}", // URL with parameters
new { controller = "fruit", action = "apples", id = UrlParameter.Optional } // Parameter defaults
);
and will respond to the url http://.../Category/Fruit/Apples/
Upvotes: 0
Reputation: 2218
Phil Haack has a route debugger on his blog.
This utility displays the route data pulled from the request of the current request in the address bar. So you can type in various URLs in the address bar to see which route matches
Upvotes: 0
Reputation: 255155
Specify default value for subcategory
routes.MapRoute(
"Category", // Route name
"Category/{category}/{subcategory}", // URL with parameters
new { controller = "Entity", action = "Category", subcategory = "Some value" } // Parameter defaults
);
Upvotes: 1
Reputation: 7750
Name is not required for routing rules. You can set it to null.
You can describe your routing with the only rule if you'll mark second parameter as optional. Or set second parameter default value as :zerkms suggests
Upvotes: 0