[英]Umbraco, get node by name and datatype
我有一個名為“Demo”的數據類型我還有一個名為“Test”的頁面/節點。如果我不在任何類型的上下文中,我將如何選擇此頁面/節點?
我已經嘗試使用新的節點(-1)並選擇我的方式,但我真的不能想出這個...
提前致謝!
最簡單的方法可能是使用以下uQuery擴展之一:
這里有幾個例子:
GetNodesByName(串):
umbraco.NodeFactory.Node node = uQuery.GetNodesByName("Page Name")
.Where(n => n.NodeTypeAlias == "NodeTypeAlias").FirstOrDefault();
if (node != null)
{
//...
}
GetNodesByType(string或int):
umbraco.NodeFactory.Node node = uQuery.GetNodesByType("NodeTypeAlias")
.Where(n => n.Name == "Page Name").FirstOrDefault();
if (node != null)
{
//...
}
有了Umbraco 6+,我認為這會更好......
public static IPublishedContent GetNodeByAlias(string alias)
{
var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
var contentNode = umbracoHelper.TypedContentSingleAtXPath(String.Format("//{0}", alias));
return contentNode;
}
這只是一個文檔類型別名,並使用當前的Umbraco上下文將您的節點查找為IPublishedContent。 UmbracoHelper也為您提供了很多選擇。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.