问题
I am trying to figure out how to correctly support returning the number of items in a (filtered) data set in my OData API.
My understanding is that adding the $count=true
argument to the query string should allow for this.
Now, based on the example from the tutorial in the official docs, adding that parameter should cause the web API to return just an integer number:
The request below returns the total number of people in the collection.
GET serviceRoot/People?$count=true
Response Payload
20
On the other hand, this accepted and quite upvoted1 answer indicates that a query with $count=true
will actually return an object, one of whose properties holds said integer number. It provides an exemplary query on a sample endpoint:
https://services.odata.org/V4/Northwind/Northwind.svc/Customers?$count=true&$top=0&$filter=Country eq 'Germany'
Indeed, the actual result from that endpoint is the complex object
{
"@odata.context": "https://services.odata.org/V4/Northwind/Northwind.svc/$metadata#Customers",
"@odata.count": 11,
"value": []
}
instead of the expected result of a single integer number
11
Why is this? Am I misunderstanding the documentation?
1: The answer had 25 upvotes at the time of writing.
来源:https://stackoverflow.com/questions/61319024/why-does-this-odata-query-with-count-true-return-an-object