使用dynamicNodeProvider的子节点

时间:2011-12-24 13:51:15

标签: asp.net-mvc-3 asp.net-mvc-5 mvcsitemapprovider

我使用this project生成站点地图路径。

在我的应用程序中我希望站点地图路径保持如下:

路径: Home > Projects > {project-name}
网址: /projects/{url-project}

路径: Home > Projects > {project-name} > Photos
网址: /projects/{url-project}/photos

路径: Home > Projects > {project-name} > Addresses
网址: /projects/{url-project}/addresses

路径: Home > Projects > {project-name} > Admin
网址: /projects/{url-project}/admin

Attemps

为此,我创建了一个 DynamicNodeProviderBase

的实现
public class ProjectDetailsDynamicNodeProvider : DynamicNodeProviderBase
{
    private readonly IProjectRepository _projectRepository;
    public ProjectDetailsDynamicNodeProvider()
    {
        _projectRepository = DependencyResolver.Current.GetService<IProjectRepository>();
    }

    #region Overrides of DynamicNodeProviderBase

    public override IEnumerable<DynamicNode> GetDynamicNodeCollection()
    {
        // Build value 
        var returnValue = new List<DynamicNode>();
        var listDB = (from p in _projectRepository.Query()
                      select new { p.Name, p.Keyword, p.Description });

        // Create a node for each project
        foreach (var project in listDB.ToList())
        {
            var node = new DynamicNode { Title = project.Name, Description = project.Description };
            node.RouteValues.Add("url", project.Keyword);

            returnValue.Add(node);
        }

        // Return 
        return returnValue;
    }

    #endregion
}

Mvc.sitemap

<mvcSiteMapNode title="Projects" controller="Project" action="Index">
  <mvcSiteMapNode title="New project" controller="Project" action="Create" />

  <mvcSiteMapNode title="Project" action="About" controller="Project" dynamicNodeProvider="BindSolution.Infra.ProjectDetailsDynamicNodeProvider, BindSolution">
    <mvcSiteMapNode title="Photos" controller="Project" action="Photos" />
    <mvcSiteMapNode title="Addresses" controller="Project" action="Addresses" />
    <mvcSiteMapNode title="Admin" controller="Project" action="Admin" />
  </mvcSiteMapNode> 
</mvcSiteMapNode>

结果

路径: Home > Projects > {project-name}
网址: /projects/{url-project}

路径: Project > Photos
网址: /projects/{url-project}/photos

路径: Project > Addresses
网址: /projects/{url-project}/addresses

路径: Project > Admin
网址: /projects/{url-project}/admin

研究

在互联网上查看I found this question on stackoverflow,但不太了解决议或不符合我的问题。 无论如何,如果这是答案,你可以给我更多关于如何实施解决方案的细节吗?

谢谢大家的帮助!

2 个答案:

答案 0 :(得分:1)

我们的代码是/ country / {countryname} / /国家/ {COUNTRYNAME} /简档

这就是我的想法。所以我会用url-project代替country。我注意到这个方法的唯一问题,所以我不知道它是否是最好的,是因为我们的国家(我们有210个)我们的内存不足。我做到这一点是状态(50)和其他几个并没有问题。我遇到了国家的问题,我们在顶部列出了9个不同的页面。当我把它保持在4时,我没有问题。希望这可以帮助。

public class CountriesDynamicNodeProvider : DynamicNodeProviderBase
{
    globalEDGEDataContext _db = new globalEDGEDataContext();
    public override IEnumerable<DynamicNode> GetDynamicNodeCollection()
    {
        var returnValue = new List<DynamicNode>();
        foreach (var category in _db.CountriesList().Select(a => a.Title))
        {
            DynamicNode node = new DynamicNode("country_" + Tags.MakeUrlFriendly(category), category);
            node.RouteValues.Add("country", Tags.MakeUrlFriendly(category));
            yield return node;
        }
    }
}

public class CountriesPagesDynamicNodeProvider : DynamicNodeProviderBase
{
    globalEDGEDataContext _db = new globalEDGEDataContext();
    public override IEnumerable<DynamicNode> GetDynamicNodeCollection()
    {
        foreach (var category in _db.CountriesList().Select(a=>a.Title))
        {
            DynamicNode node = new DynamicNode();
            node.Title = "Profile";
            node.ParentKey = "country_" + Tags.MakeUrlFriendly(category);
            node.RouteValues.Add("country", Tags.MakeUrlFriendly(category));
            yield return node;
        }
    }
}

该示例也适用于MVC5。略有修改:

  public class DepartmentEmployeeDynamicNodeProvider: DynamicNodeProviderBase
{
    AlertsContext db = new AlertsContext();

    public override IEnumerable<DynamicNode> GetDynamicNodeCollection(ISiteMapNode node)
    {
        // Build value 
        var returnValue = new List<DynamicNode>();

        // Create a node for each department 
        foreach (var department in db.Departments)
        {
            var dynNode = new DynamicNode();
            dynNode.Title = department.DepartmentName;
            dynNode.RouteValues.Add("id", department.ID);

            returnValue.Add(dynNode);
        }

        // Return 
        return returnValue; 
    }
}

答案 1 :(得分:0)

对于要在父节点下注册的子节点,只需指定其父键。