MVC3 导航和面包屑 - 数据驱动

发布于 2025-01-02 07:46:42 字数 2126 浏览 0 评论 0原文

我看过一些关于导航和面包屑的帖子,但没有发现同样的问题。但如果有的话,请原谅并指出我。谢谢。

我的情况:

Interests                   Profiles
|                       |
|_Interest Area 1               |-Profile Area 1
|   |                       |
|   |_Interest Area 1.1             |_List of Books
|       |                       |
|       |_List of Books                 |_Book Detail
|           |
|           |_Book Detail
|_Interest Area 2
    |
    |_List of Books
        |
        |_Book Detail

层次结构存储在数据库中。兴趣和个人资料的入口点是固定的。从那时起我不知道孩子们会是什么样子。

当不再有孩子时,将根据最后选择的(子)类别的值检索书籍。

我这里有两个问题: 第一:创建适当的路线。我希望的方式是:

/books/{id}/{interestareaname}/{subinterestareaname}/{books}/{page}

为用户友好的网址添加interestareaname,并且只要您沿着层次结构向下走,就会不断重复。那么输出将是: /书籍/1/项目管理/prince2/书籍/1 这显示了prince2书籍在分页列表页面第1页上。

但是当只有一个级别时,它的路线只包含一个兴趣区域名称,因此变成这样:

/book/{id}/{interestareaname}/{books}/{page}

我为解决问题1所做的事情:

    routes.MapRoute(
        "Details",
        "view/details/{id}/{booktitle}",
        new { controller = "book", action = "Details", id = "*", booktitle= "*"});

    routes.MapRoute(
        "interests",
        "books/{id}/{level1}/{level2}/{level3}/{page}",
        new { controller = "book", action = "books", level1 = "*", level2 = "*", level3 = "*", id = "0", page = UrlParameter.Optional }
    );
    routes.MapRoute(
        "profiles",
        "books/{id}/{level1}/{level2}/{level3}/{page}",
        new { controller = "book", action = "books", level1 = "*", level2 = "*", level3 = "*", id = "0", page = UrlParameter.Optional }
    );

我通过声明最大级别应该达到这一点被设置。那只是因为我不知道如何让它更有活力。

现在的问题是区分兴趣类别和个人资料类别。我通过创建相同的路线但具有不同的名称并在站点地图创建中添加路线名称来做到这一点。

我遇到的第二个问题是站点地图和面包屑。 我使用 MVCsitemapprovider 和 DynamicNodes 功能。

使用站点地图时,我在显示详细信息页面时遇到问题。因为这些书会同时出现在个人资料和兴趣上。有些书会出现在多个兴趣领域或个人资料中。 在每种情况下,我都希望看到我阅读这本书的路径。

首先,我将每本书的详细信息页面添加到站点地图中,但这太疯狂了。并创建站点地图不能很好处理的重复条目。 另一种选择是通过查询字符串将列表页面上的路由数据传递到详细信息页面,但这看起来很糟糕,然后我必须添加逻辑来解析查询字符串以模仿面包屑,这将很难,因为我只有 ID属于最低级别。

我已经度过了很多个夜晚,但我仍然无法弄清楚。如果有人可以帮助我或为我指明正确的方向,我将非常感激。

I have seen some posts about navigation and breadcrumbs but did not find the exaxt same problem. However if there is, please forgive and point me to it. Thank you.

My case :

Interests                   Profiles
|                       |
|_Interest Area 1               |-Profile Area 1
|   |                       |
|   |_Interest Area 1.1             |_List of Books
|       |                       |
|       |_List of Books                 |_Book Detail
|           |
|           |_Book Detail
|_Interest Area 2
    |
    |_List of Books
        |
        |_Book Detail

The hierarchy is stored in the database. The entry points Interests and Profiles are fixed. From there I don't know what the children will be.

When there is no child left anymore, the books get retrieved based on the value of the last selected (sub)category.

I have two problems here:
1st: Creating the appropriate routes. The way I would like it is:

/books/{id}/{interestareaname}/{subinterestareaname}/{books}/{page}

where interestareaname is added for user friendly urls and keeps getting repeated as long you go down the hierarchy. The output would then be:
/books/1/project-management/prince2/books/1
this shows the prince2 books on paged listpage page 1.

But when there is only one level it the route contains only one interestareaname and thus becomes this:

/book/{id}/{interestareaname}/{books}/{page}

What I did to solve problem number 1:

    routes.MapRoute(
        "Details",
        "view/details/{id}/{booktitle}",
        new { controller = "book", action = "Details", id = "*", booktitle= "*"});

    routes.MapRoute(
        "interests",
        "books/{id}/{level1}/{level2}/{level3}/{page}",
        new { controller = "book", action = "books", level1 = "*", level2 = "*", level3 = "*", id = "0", page = UrlParameter.Optional }
    );
    routes.MapRoute(
        "profiles",
        "books/{id}/{level1}/{level2}/{level3}/{page}",
        new { controller = "book", action = "books", level1 = "*", level2 = "*", level3 = "*", id = "0", page = UrlParameter.Optional }
    );

I got to this by stating that a max level should be set. That is only because I didnt know how to make it more dynamic.

The problem now is to differentiate between the interests categories and the profile categories. I did that by creating a same route, but with different name and added the route name in the sitemap creation.

The 2nd problem I have is the sitemap and the breadcrumbs.
I use the MVCsitemapprovider and the DynamicNodes functionality.

With the sitemap I have a problem showing the details page. Because the books will appear both on profiles and on interests. And some books will appear in multiple interest areas or profiles.
In each case I would like to see the path I used to get to that book.

First I added the detail pages for each book to the sitemap, but that was crazy. And creates duplicate entries which are not handled very well by sitemaps.
Other option was to pass the routedata on the list page to the details page by querystring, but that looked awful and I have then to add logic to parse the querystring to mimic the breadcrumb, which will be hard, since I only have the ID then of the lowest level.

I have spent now many nights and I still can´t figure it out. If some one can help or point me to the right direction I am very grateful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

无人问我粥可暖 2025-01-09 07:46:42

到目前为止,我自己找到的唯一解决方案是实际创建所有可能的路线,例如:

        routes.MapRoute(
            "interest",
            "interest",
            new { controller = "books", action = "viainterest"},
            new { controller = "books", action = "viainterest" }
        );

        routes.MapRoute(
            "interest_1_details",
            "interest/{level1}/view/{id}/{booktitle}",
            new { controller = "books", action = "detailsviainterest_1, level1 = "*", id = 0, booktitle = "*" },
            new { controller = "books", action = "detailsviainterest_1" }
        );

        routes.MapRoute(
                       "interest2details",
                       "interest/{level1}/{level2}/view/{id}/{booktitle}",
                       new { controller = "books", action = "detailsviainterest_2", level1 = "*", level2 = "*", id = 0, booktitle = "*" },
                       new { controller = "books", action = "detailsviainterest_2" }
                   );


        routes.MapRoute(
                        "interestlevel_1",
                        "interest/{id}/{level1}",
                        new { controller = "books", action = "viainterestlevel_1, level1 = "*", id = 0 },
                        new { controller = "books", action = "viainterestlevel_1"}
                    );

        routes.MapRoute(
                       "interestlevel_2",
                       "interest/{id}/{level1}/{level2}",
                       new { controller = "books", action = "viainterestlevel_2", level1 = "*", level2 = "*", id = 0 },
                       new { controller = "books", action = "viainterestlevel_2}
                   );
        //and so on for level 3 and for profiles etc.

在 DynamicNodeManager 中,我按层次添加所有类别并添加所有项目。现在我得到了一个不错的面包屑一直到详细信息页面。由于每个详细信息页面的路径(路线数据和操作)都不同,因此即使出现在多个类别中的书籍也会显示正确的面包屑。

这解决了我的问题,但我仍然认为应该有更好的方法。当前解决方案的问题是,由于详细信息项也添加到节点集合中,树会变得很大。另一个缺点是树是在应用程序启动时生成的。因此,添加一本新书不会创建节点,直到我重置 Web 应用程序。

因此,如果有其他建议,我很高兴看到它们。现在我可以继续讲内容了,这就是最后的全部内容。

The only solution I found so far for myself is to actually create all possible routes, like:

        routes.MapRoute(
            "interest",
            "interest",
            new { controller = "books", action = "viainterest"},
            new { controller = "books", action = "viainterest" }
        );

        routes.MapRoute(
            "interest_1_details",
            "interest/{level1}/view/{id}/{booktitle}",
            new { controller = "books", action = "detailsviainterest_1, level1 = "*", id = 0, booktitle = "*" },
            new { controller = "books", action = "detailsviainterest_1" }
        );

        routes.MapRoute(
                       "interest2details",
                       "interest/{level1}/{level2}/view/{id}/{booktitle}",
                       new { controller = "books", action = "detailsviainterest_2", level1 = "*", level2 = "*", id = 0, booktitle = "*" },
                       new { controller = "books", action = "detailsviainterest_2" }
                   );


        routes.MapRoute(
                        "interestlevel_1",
                        "interest/{id}/{level1}",
                        new { controller = "books", action = "viainterestlevel_1, level1 = "*", id = 0 },
                        new { controller = "books", action = "viainterestlevel_1"}
                    );

        routes.MapRoute(
                       "interestlevel_2",
                       "interest/{id}/{level1}/{level2}",
                       new { controller = "books", action = "viainterestlevel_2", level1 = "*", level2 = "*", id = 0 },
                       new { controller = "books", action = "viainterestlevel_2}
                   );
        //and so on for level 3 and for profiles etc.

In the DynamicNodeManager I add all the categories hierarchically and also add all the items. Now I get a decent breadcrumb all the way to the detail page. And since the paths (routedata and actions) for each detailpage are different, even a book that appears in several categories displays the correct breadcrumb.

This solved my problem, but I am still thinking there should a better way. The problem with the current solution is that the tree will grow huge because of the detail items that are added to the nodecollection as well. The other downside is that the tree is generated at application start. Thus adding a new book, does not create a node, until I reset the web application.

So if there are other suggestions I am happy to see them. For now I can continue with the content, which it is all about in the end.

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