简体   繁体   English

从Umbraco中的UrlName获取节点

[英]Get node from UrlName in Umbraco

I am working on an Umbraco set up ( Umbraco 7 ) and I need to be able to load a node from a node.UrlName . 我正在进行Umbraco设置( Umbraco 7 ),我需要能够从node.UrlName加载节点。

I tried Xpath but that didn't work. 我尝试了Xpath但是没有用。 Here is my xpath : 这是我的xpath

var facNode = umbraco.NodeFactory.Node.GetNodeByXpath("/*[@UrlName='" + urlName + "']");

I have also tried a few variations of this without / , with // and just looking for an id instead of a UrlName . 我还尝试了一些不带///变体,只是寻找一个id而不是UrlName

This seems to be something fairly simple but I seem to be missing something. 这似乎很简单,但是我似乎缺少了一些东西。 I am pretty new to Umbraco so any help would be greatly appreciated. 我是Umbraco新手,所以我们将不胜感激。

Id there a better way than xpath ? 有没有比xpath更好的方法? What class should I be using for this? 我应该为此使用什么课程?

Thanks in advance! 提前致谢!

Well, there are a couple of things here. 好吧,这里有几件事。 Firstly, the xpath is case-sensitive, so you need to target @urlName not @UrlName . 首先,xpath区分大小写,因此您需要定位@urlName而不是@UrlName

Also it's worth pointing out that trying to locate a node by the urlName property isn't a very good strategy because multiple nodes could have an identical urlName property. 同样值得指出的是,尝试通过urlName属性定位节点不是一个很好的策略,因为多个节点可能具有相同的urlName属性。 This is possible because Umbraco only insists that the urlName property is unique where the node shares the same parent. 这是可能的,因为Umbraco仅坚持在节点共享同一父节点的情况下urlName属性是唯一的。

This makes sense because (eg when grouping news/blog articles) you may want a node called October under a node called 2014 but also a node called October under a parent called 2013. In this scenario, your xpath would locate both October nodes and therefore not work. 这是有道理的,因为(例如,在对新闻/博客文章进行分组时),您可能希望在名为2014的节点下有一个名为October的节点,而在名为2013的父节点下又希望有一个名为October的节点。在这种情况下,您的xpath将同时定位两个October节点,因此不行。

As far as a btter way, it depends on what the context is and what you are trying to achieve. 就更好的方式而言,它取决于上下文是什么以及您要实现的目标。 You certainly should be avoiding using the umbraco.NodeFactory.Node as from Umbraco 4.8 the standard has been to use the IPublishedContent interface which is arguably more flexible. 从Umbraco 4.8开始,您当然应该避免使用umbraco.NodeFactory.Node标准是使用IPublishedContent接口,该接口可以说是更灵活的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM