0

I'm trying to build a breadcrumb trail that will be represented by a list of site map nodes. the root node will be first, then the child, grandchild....... up until the current node. I'm trying to do this with recursion, but always getting the root node only:

public static List<MvcSiteMapNode> BreadcrumbTrail(MvcSiteMapNode curr)
    {
        List<MvcSiteMapNode> t = new List<MvcSiteMapNode>();
        if (curr.ParentNode == null)
        {
            t.Add(curr);
            return t;
        }
        else
            return BreadcrumbTrail(curr.ParentNode as MvcSiteMapNode);
    }

and the caller:

var curr = SiteMap.CurrentNode as MvcSiteMapNode;

        List<MvcSiteMapNode> trail = BreadcrumbTrail(curr);
4

1 回答 1

1

您只获得根节点,因为您只添加根节点。您可以通过将当前节点添加到递归结果中来解决此问题。

public static List<MvcSiteMapNode> BreadcrumbTrail(MvcSiteMapNode curr)
    {
        List<MvcSiteMapNode> t;
        if (curr.ParentNode == null)
            t = new List<MvcSiteMapNode>();
        else
            t = BreadcrumbTrail(curr.ParentNode as MvcSiteMapNode);
        t.Add(curr);
        return t;
    }

应该做你正在寻找的

于 2013-06-20T13:51:38.960 回答