Question regarding $count in SomeOdataOpenapiExample #938
-
Hello, I was testing out the Sorry, If there is an obvious answer as to why this isn't the case, I wasn't able to find it. Thanks in advance |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
This is a great question. I believe you are asking why you don't get the If your response is: [
{...},
{...}
] The OData equivalent with {
"@count": 42,
"value": [
{...},
{...}
]
} You have a couple of options. If you're ok with the OData-style response, you can make it look like that without actually using OData. You can wrap your response with something like: public class ODataValue<T>
{
public ODataValue( IEnumerable<T> value, long count )
{
Value = value;
Count = count;
}
[JsonProperty( "@count") ]
public long Count { get; }
[JsonProperty( "value" )]
public IEnumerable<T> Value { get; }
} And then just wrap your response: return Ok(new ODataValue<Order>(orders, totalCount)); If you don't want to use the OData format, then you have a couple of other options. If you care about REST and following what HTTP already defines, then you can return GET /orders?$count=true HTTP/2
Host: localhost The server can enforce server-side paging, but still return a count with: HTTP/2 206 Partial Content
Content-Range: items 0-24/100
Content-Type: application/json
Content-Length: 420
[{...}, {...}, {...}] This would indicate to the client that they are receiving a partial response; specifically, the first 25 items (it's zero-based) out of 100. I suspect the range being returned should be known, but if it's not, you can also return Failing any of that, you can use your own custom HTTP header, but I think you'd need a compelling reason to do so. There's stuff already defined for that purpose. |
Beta Was this translation helpful? Give feedback.
This is a great question. I believe you are asking why you don't get the
@odata.count
annotation in the response when$count=true
. Correct me if I'm mistaken. This happens because you are only using some of OData. Specifically, you are only using the OData query options, you aren't using any other part of OData. In order to get an OData formatted response, you need the OData media type formatters which are part of the full OData stack. I'm not sure if you can use the OData media type formatters without an EDM; I suspect not. Even if they are supported, you should be aware that it will likely change the shape of your response.If your response is:
The OData equivalent with