从Web Api 2中的OData客户端调用补丁的正确方法是什么 [英] What is the correct way to call patch from an OData client in Web Api 2

查看:79
本文介绍了从Web Api 2中的OData客户端调用补丁的正确方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在由Web api团队创建的OData示例之后,我的控制器具有以下支持Patch的功能:

Following the OData samples created by the web api team, my controller has the following for supporting Patch:

public HttpResponseMessage Patch([FromODataUri] int key, Delta<Foo> item)
{
  var dbVersion = myDb.GetById(key);
  if(dbVersion == null)
    throw Request.EntityNotFound();

  item.Patch(dbVersion);
  myDb.Update(dbVersion);

  return Request.CreateResponse(HttpStatusCode.NoContent);
}

并使用自动生成的客户端(来自DataServiceContext),我提交如下补丁请求:

and using the auto-generated client (derived from DataServiceContext), I submit a patch request like this:

var foo = svcContainer.Foos.Where (f => f.Id == 1).SingleOrDefault();
foo.Description = "Updated Description";
svcContainer.UpdateObject(foo);
svcContainer.SaveChanges(SaveChangesOptions.PatchOnUpdate);

但是,在fiddler中跟踪调用,我看到Foo的所有其他属性都已序列化并发送到服务.那是正确的行为吗?我希望只有Id和Description通过网络发送.另外,如果我调试服务方法并调用

However, tracing the call in fiddler, I see that all other properties of Foo are serialized and sent to the service. Is that the correct behavior? I expected only the Id and Description to be sent over the wire. Also, if I debug the service method and call

GetChangedPropertyNames,返回其所有属性名称.

GetChangedPropertyNames on item, all its property names are returned.

我应该在客户端上创建某种Delta实例吗?

Should I be creating some sort of Delta instance on the client?

我了解服务的非连续性,因此服务端没有跟踪更改的上下文,但是在我看来,api团队出于某种原因增加了对补丁的支持,所以我想知道客户是否应该以其他方式调用更新.

I understand the disconnected nature of the service and thus the service side does not have a context for tracking changes, but it seems to me the api team added support for patch for a reason, so I'd like to know if the client ought to be invoking the update in a different manner.

Yiding提供的链接说明了如何从客户端创建真正的PATCH请求(使用由Microsoft.OData.Client 6.2.0及更高版本创建的Microsoft.OData.Client.DataServiceContext). 为了方便起见,下面是代码片段:

The link YiDing provided explains how to create a true PATCH request from the client (using the Microsoft.OData.Client.DataServiceContext created by the Microsoft.OData.Client 6.2.0 and above. For convenience, here is the code snippet:

var svcContainer = new Default.Container(<svcUri>);
var changeTracker = new DataServiceCollection<Foo>(svcContainer.Foos.Where(f => f.Id == 1));
changeTracker[0].Description = "Patched Description";
svcContainer.SaveChanges();

DataServiceCollection实现属性跟踪,并使用此模式,仅将更新的属性发送到服务. 不使用DataServiceCollection而仅使用

The DataServiceCollection implements property tracking, and using this pattern, only the updated properties are sent to the service. Without using DataServiceCollection and simply using

svcContainer.UpdateObject(foo);
svcContainer.SaveChanges();

尽管有相反的说明,所有属性仍然通过网络发送,至少从Microsoft.OData.Client 6.7.0

all properties are still sent over the wire despite documentation to the contrary, at least as of Microsoft.OData.Client 6.7.0

推荐答案

Microsoft.OData.Client版本6.2.0现在支持客户端属性跟踪.它将仅检测实体的修改后的属性,并以PATCH而不是PUT的形式发送更新请求,以满足您的方案要求.请参阅此博客文章以获取更多详细信息: http://blogs .msdn.com/b/odatateam/archive/2014/04/10/client-property-tracking-for-patch.aspx

The client side property tracking is now supported from Microsoft.OData.Client version 6.2.0. It will detect only the modified properties of an entity and send the update request as PATCH instead of PUT to meet the requirement of your scenario. Please refer to this blog post for more details: http://blogs.msdn.com/b/odatateam/archive/2014/04/10/client-property-tracking-for-patch.aspx

这篇关于从Web Api 2中的OData客户端调用补丁的正确方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆