Umbraco从内容中获取价值

问题描述 投票:0回答:2

在这行代码中我对这里有点困惑

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

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

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

umbraco umbraco7
2个回答
2
投票

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 NodeFactory已过时。如果您使用Umbraco 6或更高版本,我强烈建议您转换为UmbracoHelper。

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

2
投票

在剃须刀或前端代码中,始终使用UmbracoHelper

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

这将查询已发布节点的缓存

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

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

© www.soinside.com 2019 - 2024. All rights reserved.