简体   繁体   English

BreezeJS / WebApi OData最小元数据

[英]BreezeJS/WebApi OData minimal metadata

I have an WebApi OData endpoint that I'm querying with BreezeJS, and I'm having an issue with the json payload. 我有一个正在使用BreezeJS查询的WebApi OData端点,并且json有效内容存在问题。 When I query the endpoint with BreezeJS, as opposed to directly querying the endpoint via url, the json payload is ~2x, and it appears to be because the response includes full metadata. 当我使用BreezeJS查询端点时,与通过url直接查询端点相反,json有效负载为〜2x,这似乎是因为响应包含完整的元数据。

Example, querying with Breeze: 示例,使用Breeze查询:

{
        "Entity": {
            "Id": 3,
            "LegacyId": 21,
            "Type": "Company",
            "odata.id": "http://localhost/odata/Entities(3)",
            "odata.type": "Test.Models.Entity"
        },
        "Entity@odata.navigationLinkUrl": "http://localhost/odata/Answers(2)/Entity",
        "Question": {
            "Id": 1,
            "Name": "Name",
            "Type": "string",
            "odata.id": "http://localhost/odata/Questions(1)",
            "odata.type": "Test.Models.Question"
        },
        "Question@odata.navigationLinkUrl": "http://localhost/odata/Answers(2)/Question",
        "EntityId": 3,
        "Id": 2,
        "Modified": "2015-05-22T14:56:26.477",
        "Modified@odata.type": "Edm.DateTime",
        "QuestionId": 1,
        "Value": "Some Company Name",
        "odata.id": "http://localhost/odata/Answers(2)",
        "odata.type": "Test.Models.Answer"
    }

Contrasted with querying http://localhost/odata/Answers(3) : 与查询http:// localhost / odata / Answers(3)相比

{
        "Entity": {
            "Id": 3,
            "LegacyId": 21,
            "Type": "Company"
        },
        "Question": {
            "Id": 1,
            "Name": "Name",
            "Type": "string"
        },
        "EntityId": 3,
        "Id": 2,
        "Modified": "2015-05-22T14:56:26.477",
        "QuestionId": 1,
        "Value": "Some Company Name"
    }

Is there any way to return the minimal metadata through the BreezeJS query? 有什么方法可以通过BreezeJS查询返回最小的元数据吗?

You can simply mention in the accept attribute of the header of requests like this 您可以像这样简单地在请求标头的accept属性中提及

var oldClient = odatajs.oData.defaultHttpClient;            
        var myClient = {
            request: function (request, success, error) {
                //request.headers.Accept = 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8';
                var oHeaders = {
                    'Accept': 'text/html,application/xhtml+xml,application/xml,application/json;odata.metadata=minimal',                        
                };
                request.headers = oHeaders;
                return oldClient.request(request, success, error);
            }
        };

        odatajs.oData.defaultHttpClient = myClient;

A Breeze controller exposes more metadata to allow the breeze client to do advanced things like automatic client side validation depending on server side data annotations. Breeze控制器公开了更多的元数据,以允许微风客户端根据服务器端数据注释执行高级操作,例如自动客户端验证。 That's why the metadata returned by a Breeze controller needs to be much richer than the metadata returned by an OData endpoint. 这就是为什么Breeze控制器返回的元数据需要比OData端点返回的元数据丰富得多的原因。

If you don't need the extra functionality on the client side, you can make your Breeze app consume a simple OData endpoint, instead of the advanced Breeze endpoint. 如果您不需要客户端的其他功能,则可以使Breeze应用程序使用简单的OData端点,而不是高级Breeze端点。 Ie don't implement the Breeze controller on the server side, and point your breeze EntityManager directly to the OData endpoint. 即,不要在服务器端实现Breeze控制器,而是将微风EntityManager直接指向OData端点。

Here are the docs for consuming an OData endpoint directly from Breeze, without a Breeze controller 这是不使用Breeze控制器即可直接从Breeze使用OData端点文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM