Reputation: 19138
Hi im having a problem to figure this out, i am literally going nuts. I got a scenario like this. Category , Category-SubCategory, Category-SubCategory-SubSubCategory
I cant for the life of me figure out this route. My last and current is like this
routes.MapRoute(
"Navigation",
"Navigation/{nav}/{sub}/{subsub}/{id}",
new { controller = "Navigation", action = "Site", nav = UrlParameter.Optional, sub = UrlParameter.Optional, subsub = UrlParameter.Optional, id = UrlParameter.Optional }
);
I figure like this that UrlParameter.Optional would skip the sub or the subsub but instead it puts system.UrlParameter.Optional as a parameter there. Any ideas on how this can be handled?
EDIT 1:
so far i limited the site with 2 sub categories and did 3 routes and 3 actionresults. not a pretty solution but works for now
Upvotes: 1
Views: 584
Reputation: 19138
Cut it to one route by cheating a bit.
routes.MapRoute(
"navigation",
"{nav}/{name}",
new { controller = "Navigation", action = "Page", nav = UrlParameter.Optional, name = UrlParameter.Optional }
);
and i write all the categories, subcategories in the nav like
<%: Html.ActionLink("HOME", "Page", "Navigation", new { nav = "Category/Sub/", name = "Name" }, null)%>
i figure the {nav} dont really got any function here except to show the category path and in case i need to break it out ill use the string spliter.
Upvotes: 0
Reputation: 6878
I agree with @Darin that you might want to think a little more about your architecture, but I believe this series of routes would work for you:
routes.MapRoute(
"Navigation",
"Navigation/{nav}/{sub}/{subsub}/{id}",
new { controller = "Navigation", action = "Site", nav = "", sub = "", subsub = "", id = UrlParameter.Optional }
);
routes.MapRoute(
"Navigation",
"Navigation/{nav}/{sub}/{subsub}",
new { controller = "Navigation", action = "Site", nav = "", sub = "", subsub = UrlParameter.Optional, id = "" }
);
routes.MapRoute(
"Navigation",
"Navigation/{nav}/{sub}",
new { controller = "Navigation", action = "Site", nav = "", sub = UrlParameter.Optional, subsub = "", id = "" }
);
routes.MapRoute(
"Navigation",
"Navigation/{nav}",
new { controller = "Navigation", action = "Site", nav = UrlParameter.Optional, sub = "", subsub = "", id = "" }
);
Upvotes: 0
Reputation: 46008
If you don't want to use separate quesy string parameters than use a single category parameter in your route and pass multiple values comma-separated.
For example, an url /Navigation/1,2,,3
will translate to an action with one string parameter 'category' which you can than split by comma to get:
category: 1
sub-cat: 2
sub-sub-cat: empty
sub-sub-sub-cat: 3
This will allow you to change the number of subcategories without redefining your routing.
PS. Instead of comma you might want to use other character that won't be used in category names'.
Upvotes: 0
Reputation: 1038890
That's a scenario you cannot have. You can have only a single optional parameter in your route definition and this parameter should always be the LAST parameter in your route. Otherwise the routing engine cannot disambiguate between the routes and this rule has been enforced in ASP.NET MVC 3. This means that nav
, sub
and subsub
cannot be optional. You need to always provide a value for those parameters.
Consider the following urls:
Navigation/1
Navigation/1/2
It's impossible to say without ambiguity to which of your route parameters to bind 1
and 2
.
Upvotes: 1