【问题标题】:Breeze batch request with ODataConventionModelBuilder使用 ODataConventionModelBuilder 轻松批量请求
【发布时间】:2014-12-20 03:30:54
【问题描述】:

我尝试将 Breeze 与 WebAPI OData WebService 一起实现。

在服务器端我使用ODataConventionModelBuilder 和DefaultODataBatchHandler:

var builder = new ODataConventionModelBuilder();
builder.EntitySet<MyModelType>("MyModelTypes");
builder.Namespace = typeof(MyModelType).Namespace;

config.Routes.MapODataRoute(
    routeName: "ODataRoute",
    routePrefix: "api",
    model: builder.GetEdmModel(),
    batchHandler: new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer));

在客户端我使用webApiOData:

breeze.config.initializeAdapterInstance('dataService', 'webApiOData', true);

服务名称是/api,我使用服务器端元数据:

var dataService = 
    new breeze.DataService({
        serviceName: '/api', 
        hasServerMetadata: true  
    });
var manager = new breeze.EntityManager({
        dataService: dataService
    });

如果我现在尝试调用entityManager.saveChanges(),它会正确调用/api/$batch。但变更集本身会尝试向/api/MyModelTypes 发送 POST:

--batch_389a-6d15-37bd
Content-Type: multipart/mixed; boundary=changeset_a807-ab31-a36a

--changeset_a807-ab31-a36a
Content-Type: application/http
Content-Transfer-Encoding: binary

POST api/MyModelTypes HTTP/1.1
Content-ID: 1
DataServiceVersion: 2.0
Accept: application/atomsvc+xml;q=0.8, application/json;odata=fullmetadata;q=0.7, application/json;q=0.5, */*;q=0.1
Content-Type: application/json
MaxDataServiceVersion: 3.0

{"Id":-1,"Text":"Foo"}
--changeset_a807-ab31-a36a--

--batch_389a-6d15-37bd--

这导致更改集调用http://myhost/api/api/MyModelTypes 而不是http://myhost/api/MyModelTypes:

--batchresponse_4d3988b4-10e6-4df4-bae3-a1fa034a065c
Content-Type: multipart/mixed; boundary=changesetresponse_b1df8cf5-8450-43f7-9eda-    5dc6bfdb7554

--changesetresponse_b1df8cf5-8450-43f7-9eda-5dc6bfdb7554
Content-Type: application/http
Content-Transfer-Encoding: binary

HTTP/1.1 404 Not Found
Content-ID: 1
Content-Type: application/json; charset=utf-8

{"Message":"No HTTP resource was found that matches the request URI 'http://myhost/api/api/MyModelTypes'."}
--changesetresponse_b1df8cf5-8450-43f7-9eda-5dc6bfdb7554--
--batchresponse_4d3988b4-10e6-4df4-bae3-a1fa034a065c--

我需要更改哪些内容才能在批处理请求中创建 Breeze 正确链接?

【问题讨论】:

    标签: asp.net-web-api odata breeze


    【解决方案1】:

    似乎在 Microsoft.AspNet.WebApi.OData 中的 5.2.2 和 5.3.1 版本之间有时会引入此错误,因此尚未采用 BreezeJS:

    Can't save the data to OData controller after updating to Microsoft.AspNet.WebApi.OData 5.3.x

    我为 BreezeJS 添加了一个问题: https://github.com/Breeze/breeze.js/issues/42

    【讨论】:

    • 我在 Pascal 的帖子中添加了一条评论,解释了我建议你现在做什么。谢谢,帕斯卡,你解决了这个问题。
    猜你喜欢
    • 2012-12-07
    • 1970-01-01
    • 2010-12-22
    • 2017-02-19
    • 2020-02-14
    • 2014-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多