Search code examples
asp.net-mvcasp.net-web-apiodata

OData in Web API Doesn't Follow OData Standard Specs


First of, the standard spec for OData response looks like:

{
    "d" : {
        "results": [
            {
                "__metadata": {
                    "uri": "http://demos/Orders(10248)",
                    "type": "SampleModel.Order"
                },
                "ProductId": 10248,
                "Name": "Vgnon"
            }
        ],
        "__count": "1"
    }
}

But, in Web API (using EntitySetController), the OData response looks like:

    {
            "odata.metadata":"http://localhost:43111/api/$metadata#Products",
            "odata.count":"1",
            "value":
            [
                {
                  "ProductId":2,
                  "CreatedDate":"2013-06-10T23:53:26",
                  "CreatedBy":0,"Name":"Camera"
                }
            ]
        }

Notice the count in both response. Does Web API follow OData standard specs?

Also, as pointed out by this SO question: Web API OData Inlinecount not working The answer says something like:

$inlinecount out of the box only works when you're sending back OData responses.

Does that mean in Web API (using either EntitySetController or ODataController) we can return OData response and non-OData response? Or should I say, Standard OData response and non-standard OData response?


Solution

  • The WebAPI OData implementation does follow the OData specs. The JSON you included at the top of your question is an old OData JSON format, which is now usually referred to as "Verbose JSON". The JSON format that WebAPI is producing is the new OData JSON format for OData v3 and above. WCF Data Services will produce the same format for the "application/json" media type in v3 services.

    Try sending an Accept header of "application/json;odata=verbose" if you want the old format.

    Take a look at the spec here for an explanation of the new JSON format: http://docs.oasis-open.org/odata/odata-json-format/v4.0/csprd01/odata-json-format-v4.0-csprd01.html (Note this is the OData v4 spec, which is still not finalized, but most of what's described there about the JSON format applies to v3 as well).