子类路由mvc 2?
嗨,我有一个问题要解决这个问题,我真的快疯了。我有一个这样的场景。类别,类别-子类别,类别-子类别-子子类别
我一生都无法弄清楚这条路线。我的最后一个和当前的情况是这样的
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 }
);
,我认为 UrlParameter.Optional 会跳过 sub 或 subsub,而是将 system.UrlParameter.Optional 作为参数放在那里。关于如何处理这个问题有什么想法吗?
编辑1:
到目前为止,我将该网站限制为 2 个子类别,并执行了 3 条路线和 3 个操作结果。这不是一个很好的解决方案,但目前有效
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是你不可能遇到的场景。您的路由定义中只能有一个可选参数,并且此参数应始终是路由中的最后一个参数。否则,路由引擎无法消除路由之间的歧义,并且此规则已在 ASP.NET MVC 3 中强制执行。这意味着
nav
、sub
和subsub
不能是可选的。您需要始终为这些参数提供一个值。考虑以下 url:
不可能明确地说出要绑定
1
和2
的路由参数。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
andsubsub
cannot be optional. You need to always provide a value for those parameters.Consider the following urls:
It's impossible to say without ambiguity to which of your route parameters to bind
1
and2
.如果您不想使用单独的查询字符串参数,则可以在路由中使用单个类别参数并传递以逗号分隔的多个值。
例如,URL /Navigation/1,2,,3
将转换为带有一个字符串参数“category”的操作,您可以用逗号分隔该参数来获取:
这将允许您更改子类别的数量,而无需重新定义您的路由。
附言。您可能想使用不会在类别名称中使用的其他字符来代替逗号。
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:
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'.
我同意@Darin 的观点,你可能想更多地考虑你的架构,但我相信这一系列的路线适合你:
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:
通过一点作弊将其切成一条路线。
我在导航中写下了所有类别、子类别,就像
我认为 {nav} 这里实际上没有任何功能,除了显示类别路径之外,如果我需要将其分解,我会使用字符串拆分器。
Cut it to one route by cheating a bit.
and i write all the categories, subcategories in the nav like
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.