简体   繁体   English

在odata v4中返回包含多个实体类型的列表(类似于$ all)

[英]Returning a list containing multiple entity types in odata v4 (similar to $all)

I find myself needing to create an action using odata v4 (the Web Api 2 implementation) that can return either a list containing multiple different entity types OR an action that returns an object that contains many entity types. 我发现自己需要使用odata v4(Web Api 2实现)创建一个动作,该动作可以返回包含多个不同实体类型的列表,也可以返回一个包含多个实体类型的对象的动作。

However if I create an action returning a List I get an error indicating that all items in the list must be the same type (or derive from a common type). 但是,如果我创建一个返回列表的操作,则会收到一条错误消息,指出该列表中的所有项目必须为同一类型(或派生自同一类型)。 Now I am assuming that means derive from a common type registered in the entity data model because they do all inherit from object. 现在,我假设方法是从实体数据模型中注册的通用类型派生的,因为它们都从对象继承。

If I create a type to nest the various entity types I want to return in, the object that is returned has no data (my guess is because the contents are entity types it is trying to be helpful and leave them as navigation properties or something but that isn't what I need). 如果我创建一个类型来嵌套要返回的各种实体类型,则返回的对象将没有数据(我的猜测是因为内容是实体类型,它试图提供帮助并将其保留为导航属性或其他内容,但那不是我所需要的)。

As I was digging around trying to figure out how to register the action in the model I came across the $all endpoint (which doesnt appear to work in the web api 2 implementation but it is possible my custom stuff has broken it as well) http://docs.oasis-open.org/odata/odata/v4.0/errata03/os/complete/part2-url-conventions/odata-v4.0-errata03-os-part2-url-conventions-complete.html#_Toc453752355 however I dont have any clue how to do something similar from an action. 当我四处寻找试图在模型中注册动作的过程时,我遇到了$ all端点(在Web api 2实现中似乎不起作用,但是我的自定义内容也有可能破坏了它) http ://docs.oasis-open.org/odata/odata/v4.0/errata03/os/complete/part2-url-conventions/odata-v4.0-errata03-os-part2-url-conventions-complete.html #_Toc453752355但是我不知道如何从动作中执行类似操作。

i am hoping it is as easy as 我希望它像

actionConfig.ReturnsCollectionOfTypelessGoo() 

but i am willing to jump through hoops if that is necessary :) 但是我愿意跳篮球,如果有必要的话:)

The first thought is a List<object> , or better new { data = new List<object>() } since returning arrays can cause issues. 第一个想法是List<object>或更好的new { data = new List<object>() }因为返回数组可能会引起问题。


Update: You could also return json as string, if all fails. 更新:如果所有方法均失败,您也可以将json作为字符串返回。

Consider using an untyped model. 考虑使用无类型的模型。 Example project here 示例项目在这里

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

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