将参数传递给它返回一个集合中的OData(GET)方法 [英] Passing parameters to an OData (GET) method which returns a collection

查看:901
本文介绍了将参数传递给它返回一个集合中的OData(GET)方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的的OData V3 。我如何传递参数给的OData 控制器,并返回一个集合?什么我试图做的例子:

I am using OData v3. How do I pass a parameter to an OData controller and return a collection? Example of what I am trying to do:

[EnableQuery(AllowedQueryOptions = AllowedQueryOptions.All)]
public IQueryable<ServiceInfoResult> Get([FromODataUri] int instanceId)
{
    //etc
}

当我测试,我得到这个错误:

When I test this, I get this error:

No HTTP resource was found that matches the request URI 'http://localhost:30863/odata/ServiceInfoResultApi(1)'.
No action was found on the controller 'ServiceInfoResultApi' that matches the request.

我知道的 OData的动作,但他们不是在这种情况下,一个可行的选择,因为我需要获得 odata.count 返回在响应,但无论我做什么,我不能得到财产使用的的OData操作尝试(即使发生了什么这里建议时返回:的网页API的OData inlinecount不工作这里:<一href=\"http://stackoverflow.com/questions/26405822/webapi-odata-expand-with-entity-framework-functions\">Webapi ODATA扩大与实体框架功能的)。所以它似乎我唯一的选择是创建为 ServiceInfoResult 实体的新的OData API控制器,​​这样我就可以避开OData的行动。然而,正如你可以在上面看到,传递一个参数返回集合的方法似乎会导致其他错误。

I am aware of OData actions, but they are not a viable option on this case, because I need to get odata.count returned in the response, but no matter what I do, I cannot get that property returned when using OData actions (even when trying what was suggested here: Web API OData Inlinecount not working and here: Webapi odata expand with entity framework functions). So it would seem my only alternative was to create a new OData API controller for the ServiceInfoResult entity, so that I can avoid OData actions. However, as you can see above, passing in a parameter to a method that returns a collection seems to cause other errors.

任何解决方案?不,我不能更新到的OData v4的,因为这presents的其他问题(它会采取比我有更多的时间,而且,它不支持DATETIME)

Any solutions? And no, I can't update to OData v4, since that presents a whole host of other issues (it would take more time than I have and moreover, it doesn't support DateTime)

更新

由于@Fan欧阳,我发现在JSON有效载荷失踪 odata.count 的原因是,我不返回实体类型。如果 ServiceInfoResult 在我的数据库中的一个实体, odata.count 将返回。傻傻的有点,它不是被退回的只是因为这,但是这只是它是如何。我不知道是否有任何解决方法。例如,我可以下载源$ C ​​$ C,换1号线code和使用?否则,也许是时候,我开始看的OData V4。这个项目我已经是相当大的,所以这不是一个很好的想法,随着时间的短量我。所以,如果有任何替代方案,我想听到他们的声音。谢谢!

Thanks to @Fan Ouyang, I've discovered the reason for odata.count missing in the JSON payload is that I am not returning an entity type. If ServiceInfoResult were an entity in my database, odata.count would be returned. A bit silly that it's not being returned just because of that, but that's just how it is. I'm wondering if there's any workaround. For example, can I download the source code, change 1 line of code and use that? Otherwise, maybe it's time I started looking at OData v4. The project I have is quite big, so that's not a nice thought with the short amount of time I have. So, if there's any alternatives, I'd like to hear them. Thanks!

推荐答案

这是最近固定的5.7版本的发布(@ odata.count现在还会返回复杂类型的集合)。更多资讯:
https://github.com/OData/WebApi/issues/484#issuecomment-153929767

This was recently fixed with the release of version 5.7 (@odata.count is now returned for collections of complex types). More info here: https://github.com/OData/WebApi/issues/484#issuecomment-153929767

这篇关于将参数传递给它返回一个集合中的OData(GET)方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆