2015-09-27 62 views
-1

我有ASP.NET MVC项目,我有一些模块。有些模块具有分页功能。对于测试和了解MvcSiteMapProvider我有一个模块论坛工作,并创建ForumDynamicNodeProvider类DynamicNodeProviderBase +分页

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。也许有其他决定?

+0

请说明您的问题。 “有参数页”是什么意思? – NightOwl888

回答

1

对于环境上下文(例如页码),您可以使用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 
    }); 
} 

注:当您使用PreservedRouteParameters,它们包含在当前请求生成的URL,如果提供,如果不是在请求中提供不包含在URL中。因此,如果同一祖先中有多个页码,则需要为每个页面分配一个单独的路由键名称,否则当前页码将从当前请求传递给祖先节点。

+0

从文档中查看[明确设置URL](https://github.com/maartenba/MvcSiteMapProvider/wiki/Controlling-URL-Behavior#setting-urls-explicitly)。请注意,Protocol和HostName属性可以与Url属性结合使用。但是,当您使用Url属性时,Controller,Action,Area,RouteValues,Route和PreservedRouteParameters不起作用。查询字符串值是不区分大小写的字符串匹配 - 意味着当使用Url属性时,您必须按照与节点相同的顺序将它们传递到URL中。 – NightOwl888