【发布时间】:2025-12-20 07:25:07
【问题描述】:
我对这行代码有点困惑
var cs = ApplicationContext.Current.Services.ContentService.GetById(1000);
cs.GetValue("test");
var nd = new Node(1000);
nd.GetProperty("test");
这两个代码都可以使用..这两个代码有什么不同..我们何时以及为什么使用它们中的任何一个
【问题讨论】:
我对这行代码有点困惑
var cs = ApplicationContext.Current.Services.ContentService.GetById(1000);
cs.GetValue("test");
var nd = new Node(1000);
nd.GetProperty("test");
这两个代码都可以使用..这两个代码有什么不同..我们何时以及为什么使用它们中的任何一个
【问题讨论】:
Umbraco 服务
umbraco 6 中引入的新 umbraco API 的服务层包括 ContentService、MediaService、DataTypeService 和 LocalizationService。查看umbraco documentation 以获取有关这些服务和其他 umbraco 服务的文档。
umbraco 中的服务会访问数据库,并且不会利用 umbraco 提供的所有缓存。您应该谨慎使用这些服务。如果您尝试以编程方式从数据库中添加/更新/删除,或者如果您尝试从数据库中获取未发布的内容,您应该使用这些服务。如果您只需要查询已发布的内容,则应该使用 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");
【讨论】:
在 razor 或前端代码中,始终使用 UmbracoHelper
var node = Umbraco.TypedContent(1000);
var value = node.GetPropertyValue<string>("test");
这将查询缓存中的已发布节点
您想要使用 ContentService 调用来查询数据库,例如,如果您想要关于未发布节点的信息(您不想在视图中这样做)
使用 Node 对象进行查询可能是遗留问题(我从未使用过)
【讨论】: