我有一个Kendo UI网格,连接到odata CRUD服务(Web API 2.2 OData V4)。dataSource配置如下所示-所有的baseUrl都是相同的,只是HTTP动词发生了变化。
var dataSource = new kendo.data.DataSource({
type: "odata",
transport: {
read: {
beforeSend: prepareRequest,
url: baseUrl,
type: "GET",
dataType: "json"
},
update: {
beforeSend: prepareRequest,
url: function (data) {
return baseUrl + "(" + data.CategoryId + ")";
},
type: "PUT",
dataType: "json"
},
create: {
beforeSend: prepareRequest,
url: baseUrl,
type: "POST",
dataType: "json"
},
destroy: {
beforeSend: prepareRequest,
url: function (data) {
return baseUrl + "(" + data.CategoryId + ")";
},
type: "DELETE",
dataType: "json"
},
parameterMap: function (data, operation) {
if (operation == "read") {
var paramMap = kendo.data.transports.odata.parameterMap(data);
delete paramMap.$format;
delete paramMap.$inlinecount;
paramMap.$count = true;
return paramMap;
} else if (operation == "create" || operation == "update") {
delete data["__metadata"];
return JSON.stringify(data);
}
}
},
batch: false,
pageSize: 10,
serverPaging: true,
serverSorting: true,
serverFiltering: true,
sort: { field: "CategoryCode", dir: "asc" },
schema: {
data: function (data) { return data.value; },
total: function (data) { return data['@@odata.count']; },
model: {
id: "CategoryId",
fields: {
CategoryId: { editable: false, type: "number" },
CategoryCode: { editable: true, type: "string", required: true, validation: { maxlength: 2 } },
Description: { editable: true, type: "string", required: true, validation: { maxlength: 50 } },
Created: { editable: false, type: "date" },
CreatedBy: { editable: false, type: "string" },
Updated: { editable: false, type: "date" },
UpdatedBy: { editable: false, type: "string" }
}
}
},
error: function (e) {
commonNotification.hide();
commonNotification.show(getRequestError(e), "error");
},
change: function (e) {
commonNotification.hide();
if (e.action == "sync") {
commonNotification.show("@SharedResources.Changes_Saved", "success");
}
},
requestStart: function (e) {
if (e.type == "read" && this.hasChanges()) {
if (confirm("@SharedResources.Dirty_Navigation_Confirmation") == false) {
e.preventDefault();
} else {
this.cancelChanges();
}
}
}
});
一般来说,一切都很好。prepareRequest()函数用于应用自定义授权标头以及将Accept标头设置为“ application / json; odata = verbose”。
读取时,JSON响应类似于以下内容-这就是为什么dataSource.schema.data函数返回data.value的原因
{
"@odata.context":"https://localhost:44305/odata/$metadata#Categories",
"@odata.count":2,
"value":[
{
"CategoryId":1,
"CategoryCode":"01",
"Description":"Something",
"Created":"2014-08-01T11:03:30.207Z",
"CreatedBy":"DOMAIN\\User",
"Updated":"2014-09-05T14:36:22.6323744-06:00",
"UpdatedBy":"DOMAIN\\User"
},{
"CategoryId":2,
"CategoryCode":"02",
"Description":"Something Else",
"Created":"2014-08-01T11:03:35.61Z",
"CreatedBy":"DOMAIN\\User",
"Updated":"2014-08-26T16:07:29.198241-06:00",
"UpdatedBy":"DOMAIN\\User"
}
]
}
但是,当我创建或更新实体时,返回的JSON如下所示:
{
"@odata.context":"https://localhost:44305/odata/$metadata#Categories/$entity",
"CategoryId":3,
"CategoryCode":"03",
"Description":"Yet Another",
"Created":"2014-09-06T07:55:52.4933275-06:00",
"CreatedBy":"DOMAIN\\User",
"Updated":"2014-09-06T13:55:34.054Z",
"UpdatedBy":""
}
因为这不是用“值”包裹的,所以Kendo网格无法正确更新数据源。执行POST或PUT的控制器当前返回实体,如下所示:
return Created(category);
或者
return Updated(category);
通过更改对JsonResult的响应,我能够解决此问题,如下所示:
return Json(new { value = new[] { category } });
这样,一切都可以按需工作了……但是,我的HTTP响应现在是200(JsonResult似乎总是以200响应)。在理想的世界中,我可以在创建时返回201。我应该接受它可以与200一起使用吗,还是有一种简单的方法来响应201并仍然根据需要格式化JSON?似乎Web API 2允许了更多自定义的http响应,但是我的Web api 2.2控制器操作正在返回IHttpActionResult。我真的不想创建一个具有特殊返回类型的自定义类,而且我似乎无法使用Created()返回我的匿名对象。
总而言之,我真的很想只靠自己拥有的生活。但是,我将对以201返回我的匿名对象的方式,或者在kendo dataSource中接受非“值包装” json并使其适当更新数据的方式感兴趣。
这就是我最终要做的事情-我制作了自己的“ CreatedObject”方法,该方法将生成ResponseMessageResult。这将使用匿名的“值”对象包装该对象并序列化为json。然后,我可以用所需的响应代码返回它。
public ResponseMessageResult CreatedObject(string location, object createdObject)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
string json = serializer.Serialize(new { value = new[] { createdObject } });
// Create the response and add the 201 response code
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.Created);
response.Headers.Add("Location", location);
response.Content = new System.Net.Http.StringContent(json);
// return the result
return ResponseMessage(response);
}
这样就解决了Kendo数据源作为客户端的问题。但是,我不喜欢为特定客户端处理odata响应的想法。因此,我改为修改客户端以处理常规的Web API OData响应,如下所示:
schema: {
data: function (data) {
if (data.value) {
return data.value;
} else {
delete data["@@odata.context"];
return data;
}
},
total: function (data) { return data['@@odata.count']; },
model: {
etc...
现在,schema.data()函数在返回适当的数据之前检查对象是否包装在“值”中。返回创建的对象时,我必须删除@ odata.context属性,因为kendo不喜欢它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句