Umbraco 8:获取WebAPI类中特定类型的所有内容

Umbraco 8:获取WebAPI类中特定类型的所有内容,umbraco,webapi,umbraco8,Umbraco,Webapi,Umbraco8,Q)如何访问我的网站内容,例如我的WebAPI类中的图书收藏? 在一个Umbraco 8网站项目中,我在razor视图中使用以下内容来获取内容,这很好,允许我迭代收集并构建我的页面,一切都很好 @inherits Umbraco.Web.Mvc.UmbracoViewPage @using ContentModels = Umbraco.Web.PublishedModels; @using System.Text.RegularExpressions; @{ var books =

Q)如何访问我的网站内容,例如我的WebAPI类中的图书收藏?

在一个Umbraco 8网站项目中,我在razor视图中使用以下内容来获取内容,这很好,允许我迭代收集并构建我的页面,一切都很好

@inherits Umbraco.Web.Mvc.UmbracoViewPage
@using ContentModels = Umbraco.Web.PublishedModels;
@using System.Text.RegularExpressions;


@{
    var books = Model.Root().DescendantsOfType("Book")
                .Cast<Book>().Where(x => x.ShowInWebsite);
}
//...
我尝试过的其他事情:

The name 'Model' does not exist in the current context
var books = Umbraco.ContentAtRoot().DescendantsOrSelf().OfTypes("Book");
//这会产生错误:

IEnumerable<IPublishedContent>' does not contain a definition for 'DescendantsOrSelf' and the best extension method overload 'PublishedContentExtensions.DescendantsOrSelf(IPublishedContent, string)' requires a receiver of type 'IPublishedContent
IEnumerable'不包含'substantsorself'的定义和最佳扩展方法重载'publishedcontextensions'。substantsorself(ippublishedcontent,string)'需要类型为'ippublishedcontent'的接收器

您的WebApi类没有模型,但只要它继承自UmbracoApiController,您就应该能够这样做

Umbraco.ContentAtRoot()...
Umbraco.ContentAtRoot().DescendantsOrSelf().OfTypes("Book")
相反

按照你当时应该能做到的

Umbraco.ContentAtRoot()...
Umbraco.ContentAtRoot().DescendantsOrSelf().OfTypes("Book")

感谢@Jannik Anker的评论

我设法通过以下方式获得了我的收藏:

var parent = Umbraco.ContentAtRoot().First().Children().FirstOrDefault(x => x.Name == "Booklist");
if (parent != null) 
{
    var books = parent.Children();
    var test = "";
    foreach (var book in books) 
    {
        test += book.Id + ": " + book.Name + "\r\n";
    }
    return test;
}

那么,我是否可以建议您看看官方文件中提供了哪些方法?:-)除非您已经尝试过,否则也可以查看更新后的我的答案;-)我也试过了——请看我的最新问题和您建议的行的输出。这些文档也帮不了什么忙——因为它们完全没有关于webapi实现的详细信息,事实上非常糟糕。谢谢。@Dave我看不出有人试图使用我建议的线路。没有.degenantsoftype,但有一个.degenantsorself()可以与.OfTypes(“…”)组合,这很公平!当然ContentAtrot()是可枚举的,所以您必须在它之后抛出一个.First(),或者遍历它,并可能将每个子代的结果或self()等添加到您自己的集合中?