【问题标题】:Get all strongly-typed document of a certain type with Umbraco使用 Umbraco 获取特定类型的所有强类型文档
【发布时间】:2018-05-16 09:37:04
【问题描述】:

我有一个 Web 服务,我想在其中检索具有某种内容类型但作为强类型对象的所有文档。我试着用这个:

var contentType = Services.ContentTypeService.GetContentType(PensionPoint.ModelTypeAlias);
var points = Services.ContentService.GetContentOfContentType(contentType.Id).ToList();

但是,我得到了无法执行 Object.Property 的 IContent 对象。如何检索相同的项目但作为强类型对象?

【问题讨论】:

    标签: umbraco umbraco7


    【解决方案1】:

    您使用了错误的 Umbraco API,您需要使用 Umbraco Helper 来检索已发布的内容项目,而不是使用返回 IContent 项目的服务 API。

    var umbracoHelper = 
          new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
    var pensionPoints = 
          umbracoHelper.TypedContentAtRoot().First().Descendants<PensionPoint>();
    

    当然,如果您的代码在控制器或视图中,您也许可以直接获取UmbracoHelper 实例。

    另外,请注意,上面的代码假定您的内容位于单个“主”节点下。

    最后,如果您的网站有很多内容,性能可能不是很好,在这种情况下,您可能希望使用 XPATH 查询并将结果转换为您的类。

    【讨论】:

      猜你喜欢
      • 2012-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-26
      • 1970-01-01
      • 2012-01-28
      • 1970-01-01
      相关资源
      最近更新 更多