遵循OData标准规范

遵循OData标准规范

本文介绍了Web API中的OData不遵循OData标准规范的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,OData响应的标准规范如下:

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"
    }
}

但是,在Web API(使用EntitySetController)中,OData响应如下:

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"
                }
            ]
        }

请注意两个响应中的计数.Web API是否遵循OData标准规范?

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

此外,如该SO问题所指出: Web API OData Inlinecount不起作用答案是这样的:

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

这是否意味着在Web API(使用EntitySetController或ODataController)中,我们可以返回OData响应和非OData响应?还是说标准OData响应和非标准OData响应?

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?

推荐答案

WebAPI OData实现确实遵循OData规范.问题顶部包括的JSON是旧的OData JSON格式,现在通常称为详细JSON". WebAPI生成的JSON格式是OData v3及更高版本的新OData JSON格式. WCF数据服务将为v3服务中的"application/json"媒体类型产生相同的格式.

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.

如果您希望使用旧格式,请尝试发送"application/json; odata = verbose"的Accept标头.

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

在此处查看规范以了解新JSON格式的说明:(请注意,这是OData v4规范,该规范尚未最终确定,但是其中有关JSON格式的大多数描述也适用于v3.)

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).

这篇关于Web API中的OData不遵循OData标准规范的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 04:08