【发布时间】:2013-01-12 14:13:54
【问题描述】:
试图在 ASP.NET Web API 服务中使用System.Web.Http.OData.Delta 实现PATCH 方法,但似乎无法将更改应用于IEnumerable<T> 类型的属性。我正在使用 Delta 的最新 Git 版本(2012.2-rc-76-g8a73abe)。有没有人能够完成这项工作?
考虑这种数据类型,它应该可以在对 Web API 服务的 PATCH 请求中更新:
public class Person
{
HashSet<int> _friends = new HashSet<int>();
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public IEnumerable<int> Friends
{
get { return _friends; }
set
{
_friends = value != null ? new HashSet<int>(value) : new HashSet<int>();
}
}
public Person(int id, string firstName, string lastName)
{
Id = id;
FirstName = firstName;
LastName = lastName;
}
public Person()
{
}
}
此 Web API 方法通过Delta<Person> 实现对 Person 的修补:
public void Patch(int id, Delta<Person> delta)
{
var person = _persons.Single(p => p.Id == id);
delta.Patch(person);
}
如果我向服务发送带有以下 JSON 的 PATCH 请求,则应更新此人的 Friends 属性,但可惜不会发生:
{"Friends": [1]}
问题的关键在于如何让Delta更新Friends这个数据。另请参阅discussion at CodePlex。
【问题讨论】:
标签: rest asp.net-web-api odata