简体   繁体   English

Umbraco 8:在 WebAPI class 中获取对 DocumentType 定义的引用

[英]Umbraco 8: Get reference to DocumentType definition in WebAPI class

Q) How do I get a reference to a DocumentType definition in my UmbracoAPIController class, so that I can do LINQ queries on the properties?问)如何在我的 UmbracoAPIController class 中获取对 DocumentType 定义的引用,以便我可以对属性进行 LINQ 查询?

Background:背景:
I've got a WebAPI endpoint I call from JS, that fetches Book items from my DB.我有一个从 JS 调用的 WebAPI 端点,它从我的数据库中获取Book项目。 I want to filter based on an input variable, such as ISBN, in my LINQ query.我想在 LINQ 查询中根据input变量(例如 ISBN)进行过滤。 In order to do that, I need to get the DocumentType definition imported in my UmbracoAPIController class.为此,我需要在我的UmbracoAPIController class 中导入 DocumentType 定义。

Trying the below, where Book is the type I want to cast to:尝试以下方法,其中Book是我要转换为的类型:

var parent = Umbraco.ContentAtRoot().First().Children().FirstOrDefault(x => x.Name == "Booklist");
if (parent != null) 
{
    var isbn = HttpContext.Current.Request.Params["isbn"];

    var books = parent.Children().Cast<Book>().Where(b => b.Isbn == isbn);

    foreach (var book in books) 
    {
        // Do something here....
    }
}

Breaks with the error:打破错误:

 The type or namespace name 'Book' could not be found (are you missing a using directive or an assembly reference?)

Note: Please don't tell me I'm just doing everything the 'wrong' way unless you have a clear, better alternative, thank you.注意:请不要告诉我,除非您有明确、更好的选择,否则我只是在以“错误”的方式做所有事情,谢谢。

I dont know if this will help but i dont do a direct cast (haven't even tried it) and just work with the default property value direct eg我不知道这是否会有所帮助,但我不做直接转换(甚至没有尝试过),只是直接使用默认属性值,例如

var books = parent.Children().Where(page => page.HasProperty("Isbn") &&
                                        page.HasValue("Isbn") &&
                                        page.Value<string>("Isbn") == Isbn);

Interested though to find out how a direct cast work.不过有兴趣了解直接演员是如何工作的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM