什么是使用Web API进行部分更新的目前推荐的方法? [英] What's the currently recommended way of performing partial updates with Web API?

查看:262
本文介绍了什么是使用Web API进行部分更新的目前推荐的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何实现具有的ASP.NET Web API的RESTful接口部分更新?比方说,比如我们传递对象在以下结构的线:

I'm wondering how to implement partial updates with ASP.NET Web API's RESTful interface? Let's say for example we are passing objects over the wire of the following structure:

public class Person {
    public int Id { get; set; }
    public string Username { get; set; }
    public string Email { get; set; }
}

如何将一个支撑在一个时间只更新的部分,例如电子邮件属性?它是建议来实现这个通过<一个href=\"http://blogs.msdn.com/b/alexj/archive/2012/08/15/odata-support-in-asp-net-web-api.aspx\">OData和PATCH动词,或者它会更好地实现自己的PATCH?

How would one support updating just parts of a Person at a time, for example the Email property? Is it recommended to implement this via OData and the PATCH verb, or would it be better to implement PATCH oneself?

推荐答案

有在当前不支持最新的稳定的网络API(从2012年8月)的释放。所以,如果你想使用的网络API RTM,你就必须自己实现整个管道。

There is no support in the current latest stable release of Web API (from August 2012). So if all you want to use is Web API RTM, you would have to implement the whole plumbing yourself.

随着中说,OData的$ P $租赁前的包通过新的三角洲&LT支持部分更新非常漂亮; T&GT; 对象。
目前Microsoft.AspNet.WebApi.OData包已经在(0.3),并可以从这里获得RC版:<一href=\"http://www.nuget.org/packages/Microsoft.AspNet.WebApi.OData\">http://www.nuget.org/packages/Microsoft.AspNet.WebApi.OData

With that said, OData prerelease package supports partial updates very nicely through the new Delta<T> object. Currently the Microsoft.AspNet.WebApi.OData package is at RC version already (0.3) and can be obtained from here: http://www.nuget.org/packages/Microsoft.AspNet.WebApi.OData

一旦你安装了这一点,你就可以使用相应的:

Once you install that, you can then use that accordingly:

[AcceptVerbs("PATCH")]
public void Patch(int id, Delta<Person> person)
{
    var personFromDb = _personRepository.Get(id);
    person.Patch(personFromDb);
    _personRepository.Save();
}

和你把它从客户端这样的:

And you'd call it from the client like this:

$.ajax({
    url: 'api/person/1',
    type: 'PATCH',
    data: JSON.stringify(obj),
    dataType: 'json',
    contentType: 'application/json',
    success: function(callback) {            
       //handle errors, do stuff yada yada yada
    }
});

这样做的明显优势在于,它可以用于任何财产,你不必关心你是否更新电子邮件用户名或诸如此类的东西。

您可能也想看看这个帖子,因为它显示了一个非常类似的技术<一href=\"http://techbrij.com/http-patch-request-asp-net-webapi\">http://techbrij.com/http-patch-request-asp-net-webapi

You might also want to look into this post, as it shows a very similar technique http://techbrij.com/http-patch-request-asp-net-webapi

修改(详细信息):
为了的只是使用PATCH 的,你并不需要启用任何OData的关系,除了加入的OData包 - 获得进入三角洲&LT; TEntityType&GT; 对象。

EDIT (more info): In order to just use PATCH, you do not need to enable anything OData related, except for adding the OData package - to get access to the Delta<TEntityType> object.

您就可以做到这一点:

public class ValuesController : ApiController
{
    private static List<Item> items = new List<Item> {new Item {Id = 1, Age = 1, Name = "Abc"}, new Item {Id = 2, Age = 10, Name = "Def"}, new Item {Id = 3, Age = 100, Name = "Ghj"}};

    public Item Get(int id)
    {
        return items.Find(i => i.Id == id);
    }

    [AcceptVerbs("PATCH")]
    public void Patch(int id, Delta<Item> item)
    {
        var itemDb = items.Find(i => i.Id == id);
        item.Patch(itemDb);
    }
}

如果你的产品,让我们说:

If your item is, let's say:

{
    "Id": 3,
    "Name": "hello",
    "Age": 100
}

您可以补丁 / API /价值/ 3

{
    "Name": "changed!"
}

和,将正确地更新你的对象。

and that will correctly update your object.

三角洲&LT; TEntity&GT; 将跟踪的变化为您服务。它是一个动态类,充当你的类型的轻量级代理,并会了解原始的对象(即从DB)和一个客户端传递之间的差异。

Delta<TEntity> will keep track of the changes for you. It is a dynamic class that acts as a lightweight proxy for your Type and will understand the differences between the original object (i.e. from the DB) and the one passed by the client.

这会不会影响你的API的其余部分以任何方式(当然除了与较新的替换DLL文件,方便的OData软件包的依赖关系)。

This WILL NOT affect the rest of your API in any way (except of course replacing the DLLs with newer ones to facilitate the OData package dependencies).

我添加了一个示例项目演示PATCH +三角洲的工作 - 你可以在这里抓住它(it.s VS2012)<一个href=\"https://www.dropbox.com/s/hq7wt3a2w84egbh/MvcApplication3.zip\">https://www.dropbox.com/s/hq7wt3a2w84egbh/MvcApplication3.zip

I have added a sample project to demonstrate the work of PATCH + Delta - you can grab it here (it.s VS2012) https://www.dropbox.com/s/hq7wt3a2w84egbh/MvcApplication3.zip

这篇关于什么是使用Web API进行部分更新的目前推荐的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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