我有一个WebApi OData端点,我用BreezeJS查询,并且我遇到了json有效负载问题。当我使用BreezeJS查询端点时,而不是通过url直接查询端点,json有效负载是~2x,看起来是因为响应包含完整的元数据。
示例,使用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"
}
与查询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"
}
有没有办法通过BreezeJS查询返回最小元数据?
答案 0 :(得分:2)
你可以简单地在像这样的请求标题的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;
答案 1 :(得分:0)
Breeze控制器公开更多元数据,以允许breeze客户端执行高级操作,例如根据服务器端数据注释自动客户端验证。这就是为什么Breeze控制器返回的元数据需要比OData端点返回的元数据更丰富的原因。
如果您不需要客户端的额外功能,您可以使您的Breeze应用程序使用简单的OData端点,而不是高级的Breeze端点。即不要在服务器端实现Breeze控制器,并将您的微风EntityManager
直接指向OData端点。
以下是docs for consuming an OData endpoint directly from Breeze, without a Breeze controller