Umbraco - 在 C# 中查找根节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11940537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:47:39  来源:igfitidea点击:

Umbraco - Finding Root Node in C#

c#umbraco

提问by Atom

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?

我正在研究后端模块,所以Node.GetCurrent()不是一个选项。我需要找到一种方法来调用类似的东西Node currentNode = new Node(parentNodeId);并获取站点的根节点。我在 XSLT 中看到过示例,但没有看到 C# 中的示例。有谁知道我怎么能做到这一点?

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

即使只是获取根节点的 ID 以便我可以调用new Node()也会很棒。

采纳答案by E.J. Brennan

The rootnode is always available as:

根节点始终可用:

var rootNode = new Node(-1);

回答by DotNetDan

Brennan is correct,

布伦南是对的,

var rootNode = new DynamicNode(-1);

works as well!

也能用!

回答by tcmorris

Update for Umbraco 6+

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.

这仅需要一个文档类型别名,并使用当前 Umbraco 上下文找到根节点作为 IPublishedContent。UmbracoHelper 也为您提供了很多选择。

回答by tryinHard

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

Umbraco 7 更新(也可以在早期版本中使用)

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

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

有关更多信息,请查看文档 -> http://our.umbraco.org/Documentation/Reference/Querying/DynamicNode/Collections

回答by Leszek P

Umbraco 7:

翁布拉科 7:

Umbraco.TypedContentAtRoot();

回答by Gcamara14

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.

我经常使用这个。我喜欢它是相对的,所以如果你有多个根节点,你可以在没有 foreach 循环的情况下将它们作为目标。

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