Umbraco - Finding Root Node in C#

眉间皱痕 提交于 2019-12-08 15:19:11

问题


I'm working on a backend module, so Node.GetCurrent() is not an option. I need to find a way to call something like Node currentNode = new Node(parentNodeId); and get the root node of the site. I've seen samples in XSLT, but nothing for C#. Does anyone know how I can accomplish this?

Even just getting the ID of the root node so I can call new Node() would be great.


回答1:


The rootnode is always available as:

var rootNode = new Node(-1);



回答2:


Update for Umbraco 7 (may work in earlier versions too)

@{
    var siteroot = CurrentPage.AncestorOrSelf(1);
}

For further info, check out the documentation -> http://our.umbraco.org/Documentation/Reference/Querying/DynamicNode/Collections




回答3:


Update for Umbraco 6+

public static IPublishedContent GetRootNode()
{
    var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
    var rootNode = umbracoHelper.TypedContentSingleAtXPath("//root"));

    return rootNode;
}

This just takes a document type alias and finds the root node as IPublishedContent using the current Umbraco context. UmbracoHelper gives you quite a few options off this also.




回答4:


Brennan is correct,

var rootNode = new DynamicNode(-1);

works as well!




回答5:


Umbraco 7:

Umbraco.TypedContentAtRoot();



回答6:


I frequently use this one. I like that it's relative so that if you have multiple root nodes you can target both without a foreach loop.

IPublishedContent topNode = Model.Content.AncestorOrSelf(1);


来源:https://stackoverflow.com/questions/11940537/umbraco-finding-root-node-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!