DynamicNodeProviderBase + 分页

DynamicNodeProviderBase + Pagination

我有 ASP.NET 个 MVC 项目和一些模块。有些模块有分页。为了测试和理解 MvcSiteMapProvider 我使用了一个模块论坛并创建了 ForumDynamicNodeProvider class

public class ForumDynamicNodeProvider : DynamicNodeProviderBase
{
    private readonly IForumsService _forumsService;

    public ForumDynamicNodeProvider(IForumsService forumsService)
    {
        this._forumsService = forumsService;
    }

    public override IEnumerable<DynamicNode> GetDynamicNodeCollection(ISiteMapNode node)
    {
        string rootTitle = ManagerLocalization.Get("Forums", "FORUMS");


        var nodes = new List<DynamicNode>
                        {
                            new DynamicNode
                                {
                                    Title = rootTitle,
                                    Controller = "Forums",
                                    Action = "Index",
                                    Key = "forum_home"
                                }
                        };

        var forums = this._forumsService.GetForums<ForumNode>().ToList();
        var topics = this._forumsService.GetTopics<TopicNode>().ToList();

        foreach (var forum in forums)
        {
            var parentForum = this.GetParentForum(forums, forum);
            string parentKey = parentForum?.Id.ToString() ?? "home";

            var forumRouteValue = new Dictionary<string, object> { { "forumName", forum.NameTranslit } };

            nodes.Add(new DynamicNode
            {
                Key = $"forum_{forum.Id}",
                ParentKey = $"forum_{parentKey}",
                Title = forum.Name,
                Controller = "Forums",
                Action = "ShowForum",
                RouteValues = forumRouteValue
            });
        }

        foreach (var topic in topics)
        {
            var forum = forums.FirstOrDefault(item => item.Id == topic.ForumId);
            var forumRouteValue = new Dictionary<string, object> { { "forum", forum.NameTranslit },  { "topicName", topic.TitleTranslite }, {"page", 0 } };

            nodes.Add(new DynamicNode
            {
                Key = $"topic_{topic.Id}",
                ParentKey = $"forum_{topic.ForumId}",
                Title = topic.Title,
                Controller = "Forums",
                Action = "ShowTopic",
                RouteValues = forumRouteValue
            });
        }

        return nodes;
    }

    private ForumNode GetParentForum(List<ForumNode> forums, ForumNode forum)
    {
        if (forum.ForumId > 0)
        {
            return forums.FirstOrDefault(item => item.Id == forum.ForumId);
        }

        return null;
    }
}

但是我找不到一个好的分页决定。为了简单起见,我可以使用页面前缀作为键并制作重复的 DynamicNode。但这是个坏主意,因为当我有 1000 个主题示例并且每个主题有 20 页时,我必须创建 20000 个 DynamicNode。也许有其他决定?

对于环境上下文(例如页码),您可以对指定键的任何值使用 PreservedRouteParametersforce a match。这些键与请求中的路由值或查询字符串参数匹配(如果它们相同,则路由值优先)。

foreach (var forum in forums)
{
    var parentForum = this.GetParentForum(forums, forum);
    string parentKey = parentForum?.Id.ToString() ?? "home";

    var forumRouteValue = new Dictionary<string, object> { { "forumName", forum.NameTranslit } };

    // Always match the "page" route value regardless of its value
    var forumPreservedRouteParameters = new List<string>() { "page" };

    nodes.Add(new DynamicNode
    {
        Key = $"forum_{forum.Id}",
        ParentKey = $"forum_{parentKey}",
        Title = forum.Name,
        Controller = "Forums",
        Action = "ShowForum",
        RouteValues = forumRouteValue,
        PreservedRouteParameters = forumPreservedRouteParameters
    });
}

NOTE: When you use PreservedRouteParameters, they are included in the generated URL from the current request if provided and not included in the URL if not provided in the request. Therefore, if you have more than one page number in the same ancestry you need to have a separate route key name for each one or the current page number will be passed to the ancestor nodes from the current request.