子类路由mvc 2?

发布于 2024-10-14 21:50:48 字数 623 浏览 5 评论 0原文

嗨,我有一个问题要解决这个问题,我真的快疯了。我有一个这样的场景。类别,类别-子类别,类别-子类别-子子类别

我一生都无法弄清楚这条路线。我的最后一个和当前的情况是这样的

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

瞄了个咪的 2024-10-21 21:50:48

这是你不可能遇到的场景。您的路由定义中只能有一个可选参数,并且此参数应始终是路由中的最后一个参数。否则,路由引擎无法消除路由之间的歧义,并且此规则已在 ASP.NET MVC 3 中强制执行。这意味着 navsubsubsub 不能是可选的。您需要始终为这些参数提供一个值。

考虑以下 url:

Navigation/1
Navigation/1/2

不可能明确地说出要绑定 12 的路由参数。

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.

余厌 2024-10-21 21:50:48

如果您不想使用单独的查询字符串参数,则可以在路由中使用单个类别参数并传递以逗号分隔的多个值。

例如,URL /Navigation/1,2,,3

将转换为带有一个字符串参数“category”的操作,您可以用逗号分隔该参数来获取:

category: 1
sub-cat: 2
sub-sub-cat: empty
sub-sub-sub-cat: 3

这将允许您更改子类别的数量,而无需重新定义您的路由。

附言。您可能想使用不会在类别名称中使用的其他字符来代替逗号。

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'.

为你鎻心 2024-10-21 21:50:48

我同意@Darin 的观点,你可能想更多地考虑你的架构,但我相信这一系列的路线适合你:

 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 = "" }
);

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 = "" }
);
a√萤火虫的光℡ 2024-10-21 21:50:48

通过一点作弊将其切成一条路线。

routes.MapRoute(
            "navigation",
            "{nav}/{name}",
            new { controller = "Navigation", action = "Page", nav = UrlParameter.Optional, name = UrlParameter.Optional }
        );

我在导航中写下了所有类别、子类别,就像

<%: Html.ActionLink("HOME", "Page", "Navigation", new { nav = "Category/Sub/", name = "Name" }, null)%>

我认为 {nav} 这里实际上没有任何功能,除了显示类别路径之外,如果我需要将其分解,我会使用字符串拆分器。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文