1
votes

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.

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.

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)

UPDATE

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!

2

2 Answers

1
votes

Try this sample, CheckoutMany method take parameter and return a collection https://github.com/OData/ODataSamples/tree/master/WebApi/v3/ODataActionsSample

Add [EnableQuery] attribute in the CheckOutMany action method, and add $inlinecount queryoption in requset url, you can see odata.count in payload

By the way, V4 support datetime now: http://odata.github.io/WebApi/#04-01-datetime-support

0
votes

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