如何生成站点地图?(asp.net mvc3)

时间:2012-05-15 10:34:51

标签: asp.net asp.net-mvc-3 menu sitemap

这是我的数据库表

DB Table

如何生成站点地图?

控制器是类别

行动是Id

看起来像是

Page


TreeMenu.cs:

using System;
using System.Collections.Generic;

namespace Pmvc.Models
{
    public partial class TreeMenu
    {
        public int TreeId { get; set; }
        public int TreeParentId { get; set; }
        public string TreeName { get; set; }
        public List<TreeMenu> Children { get; set; }
    }

}

StoreDetailsDynamicNodeProvider.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcSiteMapProvider.Extensibility;
using Pmvc.Models;


namespace Pmvc
{
    public class StoreDetailsDynamicNodeProvider : DynamicNodeProviderBase
    {
        PMVCEntities pmvcDB = new PMVCEntities();
        public override IEnumerable<DynamicNode> GetDynamicNodeCollection()
        {

            var nodes = new List<DynamicNode>();

            foreach (var treemenu in pmvcDB.TreeMenu)
            {
                nodes.Add(CreateNode(treemenu));
            }

            return nodes;
        }

        private DynamicNode CreateNode(TreeMenu treemenu)
        {

            DynamicNode node = new DynamicNode();
            node.Action = "ProductDetails";
            node.Controller = "Product";
            node.Title = treemenu.TreeName;

            if (treemenu.Children != null)
            {
                foreach (var child in treemenu.Children)
                {
                    node.Children.Add(CreateNode(child)); //This line is wrong!
                }
            }
            return node;

        }

    }


}

错误:

'MvcSiteMapProvider.Extensibility.DynamicNode'不包含'儿​​童'定义,也没有找到扩展方法'儿童'


public class StoreDetailsDynamicNodeProvider : DynamicNodeProviderBase 
    {
        PMVCEntities pmvcDB = new PMVCEntities();
        public override IEnumerable<DynamicNode> GetDynamicNodeCollection()
        {
            // Build value 
            var returnValue = new List<DynamicNode>();

            // Create a node for each album 
            foreach (var treemenu in pmvcDB.TreeMenu)
            {
                DynamicNode node = new DynamicNode();
                node.Action = "ProductDetails";
                node.Controller = "Product";
                node.Title = treemenu.TreeName;
                node.RouteValues.Add("id", treemenu.TreeId);
                returnValue.Add(node);
            }

            // Return 
            return returnValue;
        } 
    }

这是我的表

TreeId TreeParentId TreeName

1 0类别1

2 1菜单项X

3 1菜单项Y

4 1菜单项Z

5 0类别2

6 5其他菜单1

7 5其他菜单2

8 0空类别

9 7菜单Lvl 3

如何编写子节点代码?

1 个答案:

答案 0 :(得分:1)

看看MvcSitemap provider。编写从数据库中读取的dynamic provider非常容易。

编辑 - 您是否阅读了文档或尝试实施任何内容?从他们的网站几乎逐字复制:

在站点地图配置文件中,在适当的位置添加动态节点:

<mvcSiteMapNode 
    title="Details" 
    action="Details"   
    dynamicNodeProvider="MyProjectName.MyDynamicNodeProvider, MyProjectName" />

然后,编写实际的节点提供程序:

public class MyDynamicNodeProvider
    : DynamicNodeProviderBase 
{
    public override IEnumerable<DynamicNode> GetDynamicNodeCollection() 
    {
        // Build value 
        var returnValue = new List<DynamicNode>();

        // ... Here, get values from your database and build up
        // the list of nodes.  They can be in a tree structure
        // too since it appears that's how your data is - just
        // build the nodes in that way with sub-lists.

        // Return 
        return returnValue; 
    } 
}

就是这样。

编辑2 - 解决其他答案。

您提供的代码会使树结构变平。在您的原始帖子中,您显示您已经能够print the tree structure。无论您在视图中使用何种代码,都可以应用深度优先遍历的相同原则。

以防这是一个模型而不是实际的代码,这里有一些快速的伪代码来演示深度优先遍历。

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

    // Get all categories without parents.
    var rootCategories = db.GetRootCategories();  

    // Loop all root level categories, creating a node for each and adding
    // to the return value.
    foreach (var category in rootCategories)
    {
        nodes.Add(CreateNode(category));
    }
    return nodes;
}

private DynamicNode CreateNode(Category category)
{
    // Create a new node for the category
    DynamicNode node = new DynamicNode();

    node.Name = category.Name;
    // ... set node properties here ...

    // If the category has children, then continue down the tree
    // and create nodes for them.  This will continue recursively
    // to the bottom of the tree.
    // Note that I'm just guessing on the property names because
    // you didn't show us the code for what your entity actually
    // looks like.  This is why you should always show what code
    // you've attempted - you can get better, more precise answers
    // instead of complete guesses.
    if (category.Children != null)
    {
        foreach (var child in category.Children)
        {
            // For each child, recursively branch into them.
            node.Children.Add(CreateNode(child));
        }
    }

    return Node;
}

请注意,这是无法测试或研究的 - 它只是显示树遍历。