Umbraco 从内容中获取价值

Umbraco Getting Value from Content

我对这行代码有点困惑

var cs = ApplicationContext.Current.Services.ContentService.GetById(1000);
cs.GetValue("test");

var nd = new Node(1000);
nd.GetProperty("test");

这两个代码都可以使用。这两个代码有什么不同。我们何时以及为什么使用其中一个

在 razor 或前端代码中,始终使用 UmbracoHelper

var node = Umbraco.TypedContent(1000);
var value = node.GetPropertyValue<string>("test");

这将在缓存中查询已发布 个节点

您想使用 ContentService 调用来查询数据库,例如,如果您想要有关未发布节点的信息(您不想在您的视图中执行此操作)

使用 Node 对象进行查询可能是遗留问题(我从未使用过)

Umbraco 服务
umbraco 6中引入的新umbracoAPI的服务层包括ContentService、MediaService、DataTypeService和LocalizationService。查看 umbraco documentation 以获取有关这些服务和其他 Umbraco 服务的文档。

umbraco 中的服务访问数据库并且不利用 umbraco 提供的所有缓存。您应该谨慎使用这些服务。如果您尝试以编程方式 add/update/delete 从数据库中获取,或者如果您尝试从数据库中获取未发布的内容,则应使用这些服务。如果您只需要查询已发布的内容,您应该使用 UmbracoHelper,因为它更快。

var cs = ApplicationContext.Current.Services.ContentService.GetById(1000);
cs.GetValue("test");

UmbracoHelper
当你想从 umbraco 查询内容时,你几乎应该总是使用 UmbracoHelper。它不会访问数据库并且比 umbraco 服务快得多。

var node = Umbraco.TypedContent(1000);
var nodeVal = node.GetPropertyValue<string>("test");

如果你发现你没有权限访问 UmbracoHelper,只要你有一个 UmbracoContext,你可以自己制作:

var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
var node = Umbraco.TypedContent(1000);
var nodeVal = node.GetPropertyValue<string>("test");

节点工厂
NodeFactory 已过时。如果您使用的是 Umbraco 6 或更高版本,我强烈建议您转换为 UmbracoHelper。

var nd = new Node(1000);
nd.GetProperty("test");