2015-05-28 60 views
0

我有一個WebApi OData端點,我用BreezeJS查詢,而且我遇到了json有效負載的問題。當我用BreezeJS查詢端點時,與通過url直接查詢端點相反,json有效負載是〜2x,並且這似乎是因爲響應包含完整的元數據。BreezeJS/WebApi OData最小元數據

例如,微風查詢:

{ 
     "Entity": { 
      "Id": 3, 
      "LegacyId": 21, 
      "Type": "Company", 
      "odata.id": "http://localhost/odata/Entities(3)", 
      "odata.type": "Test.Models.Entity" 
     }, 
     "[email protected]": "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" 
     }, 
     "[email protected]": "http://localhost/odata/Answers(2)/Question", 
     "EntityId": 3, 
     "Id": 2, 
     "Modified": "2015-05-22T14:56:26.477", 
     "[email protected]": "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

Breeze控制器公開更多的元數據以允許breeze客戶端執行高級的事情,例如自動客戶端驗證,具體取決於服務器端數據註釋。這就是爲什麼Breeze控制器返回的元數據需要比OData端點返回的元數據豐富得多的原因。

如果您不需要客戶端的額外功能,您可以讓Breeze應用程序使用簡單的OData端點,而不是先進的Breeze端點。即請勿在服務器端實施Breeze控制器,並將您的微風EntityManager直接指向OData端點。

這裏是docs for consuming an OData endpoint directly from Breeze, without a Breeze controller

2

您可以請求的報頭的這樣

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; 
的接受屬性簡單地提