简体   繁体   English

使用ASP.NET Web API进行部分更新

[英]partial update with asp.net web api

I created a simple todo list application in asp.net mvc 3 with web api and dbContext. 我使用Web api和dbContext在asp.net mvc 3中创建了一个简单的待办事项列表应用程序。 ( with backbone and requirejs for the client ) Everything works fine, but i am sort of bothered with the fact that i have to sent the entire model to the server if i check or uncheck a todo item as done. (使用客户端的骨干网和requirejs)一切正常,但是如果我检查或取消检查待办事项,必须将整个模型发送到服务器,这一点令我感到困扰。 I would like to only sent the "done" field when submitting data. 我只想在提交数据时发送“完成”字段。

I should mention that i'm also using the JsonNetFormatter to use JSON.NET as the default Serializer ( explained here: http://blogs.msdn.com/b/henrikn/archive/2012/02/18/using-json-net-with-asp-net-web-api.aspx ). 我应该提到的是,我还使用JsonNetFormatter将JSON.NET用作默认的序列化器(在此处说明: http : //blogs.msdn.com/b/henrikn/archive/2012/02/18/using-json- net-with-asp-net-web-api.aspx )。

Currently this is my api controller method to update the model 目前,这是我用于更新模型的api控制器方法

public HttpResponseMessage Put(Todo todo)
{
    _db.Entry(todo).State = EntityState.Modified;
    _db.SaveChanges();
    return new HttpResponseMessage(HttpStatusCode.NoContent);
}

It takes this as json data 将其作为json数据

{"content":"Pick up milk","done":false,"id":10}

Off course this works, but it is updating the entire model, it should only update 1 field. 当然这是可行的,但是它正在更新整个模型,它应该只更新1个字段。 I can achieve to only send the changed fields to the server from the browser, but i am not sure what the web api method should look like. 我可以实现只将更改的字段从浏览器发送到服务器,但是我不确定Web api方法应该是什么样。 I was thinking about doing something with FormCollection but this doesn't seem to work with the web api as it appears to be trying to serialize the submitted formvalues directly to FormCollection type, I get this error. 我正在考虑对FormCollection进行操作,但这似乎不适用于Web api,因为它似乎试图将提交的formvalues直接序列化为FormCollection类型,但出现此错误。

Cannot deserialize JSON object (i.e. {"name":"value"}) into type 'System.Web.Mvc.FormCollection'.

How can i send a partial update for 1 or more fields from a model to my web api ? 如何将模型的1个或多个字段的部分更新发送到Web API? I only want to send the updated fields to the server, and from there only update those fields to the database. 我只想将更新的字段发送到服务器,然后从那里仅将那些字段更新到数据库。 I certainly do not want to query the database before updating. 我当然不想在更新之前查询数据库。

One approach would be to use a tool called Automapper and configure it so that null values don't overwrite existing ones when mapping Todo objects. 一种方法是使用一个名为Automapper的工具并对其进行配置,以使在映射Todo对象时,空值不会覆盖现有值。 For example: 例如:

  Mapper.CreateMap<Todo,Todo>()
        .ForMember(d => d.Id, o => o.Ignore())
        .ForAllMembers(mo => mo.Condition(cond => !cond.IsSourceValueNull));  

Then you would just have to map the received object value to the existing one, like this: 然后,您只需要将接收到的对象值映射到现有对象值即可,如下所示:

  Mapper.Map(todo, item);

Another suggestion would be to use PATCH instead of PUT which is more appropriate to partial updates of resources according to REST . 另一个建议是使用PATCH代替PUT,这更适合根据REST对资源进行部分更新

You need to query the original object from the database, set its properties & call the _db.SaveChange() 您需要从数据库中查询原始对象,设置其属性并调用_db.SaveChange()

public HttpResponseMessage Put(Todo todo){
var item = _db.Todo.First(i => i.id = todo.id);
item.Content = todo.Content;
item.Done = todo.Done;
_db.SaveChanges();
return new HttpResponseMessage<Todo>(HttpStatusCode.Accepted);
}

Ref.: http://msdn.microsoft.com/en-us/library/dd456854.aspx 参考: http : //msdn.microsoft.com/en-us/library/dd456854.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM