gpt4 book ai didi

odata - 使用 Breezejs 发布批量请求

转载 作者:行者123 更新时间:2023-12-01 09:59:14 24 4
gpt4 key购买 nike

我一直在尝试使用 Breezejs 和 WebAPI OData Controller 发布一个实体。
配置如下:

config.Routes.MapODataRoute(
routeName: "odata",
routePrefix: "odata",
model: model,
batchHandler: new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer));

模型非常直接:

public class ServiceMetadata
{
public int ServiceMetadataId { get; set; }
public string ServiceName { get; set; }
public string Description { get; set; }
public ObjectState? State { get; set; }
public DateTime? LastUpdated { get; set; }
}

并且是通过默认映射的:

ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();

客户端 也非常简单,使用 AngularJs 并部分来自 Todo 示例:http://www.breezejs.com/samples/todo-angular

breeze.config.initializeAdapterInstance("modelLibrary", "backingStore", true);
var serviceName = 'http://localhost:8081/odata/';
breeze.config.initializeAdapterInstances({ dataService: "OData" });
var manager = new breeze.EntityManager(serviceName);
manager.enableSaveQueuing(true);

实际发布是使用默认的 createEntity() 方法完成的:

function createServiceMetadata(initialValues) {
return manager.createEntity('ServiceMetadata', initialValues);
}

整个事情看起来像:

 serviceMetadatas.createServiceMetadata({
ServiceName: $scope.newServiceName,
Description: $scope.newServiceDescription
});

serviceMetadatas.saveChanges();


但是,请求没有被传输到正确的 Controller (从 EntitySetController 继承的 ServiceMetadatasController),或与此相关的任何其他 Controller 。
HTTP 请求如下所示:

    POST http://localhost:8081/odata/$batch HTTP/1.1
Host: localhost:8081
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0
Accept: multipart/mixed
Accept-Language: he-IL,he;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
DataServiceVersion: 2.0
Content-Type: multipart/mixed; charset=UTF-8;boundary=batch_4f09-d7cf-dd99
MaxDataServiceVersion: 2.0
Referer: http://localhost:9000/
Content-Length: 580
Origin: http://localhost:9000
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache


--batch_4f09-d7cf-dd99
Content-Type: multipart/mixed; boundary=changeset_ca0c-06b7-ddbe

--changeset_ca0c-06b7-ddbe
Content-Type: application/http
Content-Transfer-Encoding: binary

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

{"ServiceMetadataId":-1,"ServiceName":"sdf sdf","Description":"sd fgs df","LastUpdated":null}
--changeset_ca0c-06b7-ddbe--

--batch_4f09-d7cf-dd99--

响应:

    HTTP/1.1 202 Accepted
Cache-Control: no-cache
Pragma: no-cache
Content-Type: multipart/mixed; boundary=batchresponse_966d4460-e00e-4900-b1c9-85b17081cfac
Expires: -1
Server: Microsoft-IIS/8.0
Access-Control-Allow-Origin: http://localhost:9000
Access-Control-Allow-Credentials: true
DataServiceVersion: 2.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcVG9tZXJcRG9jdW1lbnRzXFZpc3VhbCBTdHVkaW8gMjAxMlxQcm9qZWN0c1xFYXN5Qml6eVxFYXN5Qml6eS5XZWJBUElcb2RhdGFcJGJhdGNo?=
X-Powered-By: ASP.NET
Date: Sun, 15 Sep 2013 14:32:39 GMT
Content-Length: 443

--batchresponse_966d4460-e00e-4900-b1c9-85b17081cfac
Content-Type: multipart/mixed; boundary=changesetresponse_44da5dcf-877d-4041-a82b-c51d06a4e9a4

--changesetresponse_44da5dcf-877d-4041-a82b-c51d06a4e9a4
Content-Type: application/http
Content-Transfer-Encoding: binary

HTTP/1.1 406 Not Acceptable
Content-ID: 1


--changesetresponse_44da5dcf-877d-4041-a82b-c51d06a4e9a4--
--batchresponse_966d4460-e00e-4900-b1c9-85b17081cfac--

知道黑客是怎么回事吗?B.T.W GET 请求效果很好。

附言在看了几个演示之后,我认为考虑到 WebApi 和 OData,使用 BreezeJS 会很直接。
我必须说,配置此 JS 库并不容易。我希望它会变得难以设置但易于使用。

谢谢。

@UPDATE 查看 Javier 的精彩回答!!

在挖掘 allllot 上的 breeze 代码后,我开始意识到问题出在 breezejs 的 createChangeRequests() 深处,就在这里:

request.requestUri = entity.entityType.defaultResourceName;

由于某些原因,defaultResouceName 完全忽略了该实体的路径。长话短说,下面是一个黑客要解决的问题:

manager.metadataStore.getEntityType(ENTITY_TYPE).setProperties({defaultResourceName: THE_MISSING_PART_FROM_THE_URL + ENTITY_TYPE});
manager.createEntity(ENTITY_TYPE, values);

不是很好,但仍然有效!

最佳答案

新的答案是 .. 使用 webApiOData 数据服务而不是“OData”:

breeze.config.initializeAdapterInstances({ 
dataService : 'webApiOData'
});

关于odata - 使用 Breezejs 发布批量请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18813890/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com