使用ODataConventionModelBuilder进行Breeze批量请求

时间:2014-10-24 09:44:05

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

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

在服务器端,我使用ODataConventionModelBuilderDefaultODataBatchHandler

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。但是变更集本身会尝试POST到/api/MyModelTypes

--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正确的链接?

1 个答案:

答案 0 :(得分: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

相关问题