Web API 2-实施PATCH [英] Web API 2 - Implementing a PATCH

查看:82
本文介绍了Web API 2-实施PATCH的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个实现RESTFul API的Web API.我的API的模型如下所示:

I currently have a Web API that implements a RESTFul API. The model for my API looks like this:

public class Member
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime Created { get; set; }
    public DateTime BirthDate { get; set; }
    public bool IsDeleted { get; set; }
}

我已经实现了PUT方法来更新与此类似的行(为简便起见,我省略了一些无关的内容):

I've implemented a PUT method for updating a row similar to this (for brevity, I've omitted some non-relevant stuff):

[Route("{id}")]
[HttpPut]
public async System.Threading.Tasks.Task<HttpResponseMessage> UpdateRow(int id, 
    [FromBody]Models.Member model)
{
    // Do some error checking
    // ...
    // ...

    var myDatabaseEntity = new BusinessLayer.Member(id);
    myDatabaseEntity.FirstName = model.FirstName;
    myDatabaseEntity.LastName = model.LastName;
    myDatabaseEntity.Created = model.Created;
    myDatabaseEntity.BirthDate = model.BirthDate;
    myDatabaseEntity.IsDeleted = model.IsDeleted;

    await myDatabaseEntity.SaveAsync();
}

使用 PostMan ,我可以发送以下JSON,并且一切正常:

Using PostMan, I can send the following JSON and everything works fine:

{
    firstName: "Sara",
    lastName: "Smith",
    created: '2018/05/10",
    birthDate: '1977/09/12",
    isDeleted: false
}

如果我以PUT请求的形式将其作为主体发送到http://localhost:8311/api/v1/Member/12,则ID为12的数据中的记录将更新为您在JSON中看到的记录.

If I send this as my body to http://localhost:8311/api/v1/Member/12 as a PUT request, the record in my data with ID of 12 gets updated to what you see in the JSON.

我想做的是实现一个PATCH动词,在其中我可以进行部分更新.如果Sara结婚,我希望能够发送此JSON:

What I would like to do though is implement a PATCH verb where I can do partial updates. If Sara gets married, I would like to be able to send this JSON:

{
    lastName: "Jones"
}

我希望能够仅发送JSON并仅更新LastName字段,而将所有其他字段保留下来.

I would like to be able to send just that JSON and update JUST the LastName field and leave all the other fields alone.

我尝试过:

[Route("{id}")]
[HttpPatch]
public async System.Threading.Tasks.Task<HttpResponseMessage> UpdateRow(int id, 
    [FromBody]Models.Member model)
{
}

我的问题是,这将返回model对象中的所有字段(除LastName字段外,所有字段均为空),这是有道理的,因为我说的是我想要的是Models.Member对象.我想知道的是,是否有一种方法可以检测到JSON请求中实际发送了哪些属性,以便我可以仅更新那些字段?

My problem is that this returns all the fields in the model object (all of them are nulls except the LastName field), which makes sense since I am saying I want a Models.Member object. What I would like to know is if there is a way to detect which properties have actually been sent in the JSON request so I can update just those fields?

推荐答案

PATCH操作通常不使用与POSTPUT操作相同的模型来定义,正是由于以下原因: nulldon't change. 从IETF :

PATCH operations aren't usually defined using the same model as the POST or PUT operations exactly for that reason: How do you differentiate between a null, and a don't change. From the IETF:

但是,对于PATCH,封闭的实体包含一组 说明当前资源如何驻留在 应该修改原始服务器以产生新版本.

With PATCH, however, the enclosed entity contains a set of instructions describing how a resource currently residing on the origin server should be modified to produce a new version.

您可以在此处中查找他们的PATCH建议,但sumarilly是:

You can look here for their PATCH suggestion, but sumarilly is:

[
    { "op": "test", "path": "/a/b/c", "value": "foo" },
    { "op": "remove", "path": "/a/b/c" },
    { "op": "add", "path": "/a/b/c", "value": [ "foo", "bar" ] },
    { "op": "replace", "path": "/a/b/c", "value": 42 },
    { "op": "move", "from": "/a/b/c", "path": "/a/b/d" },
    { "op": "copy", "from": "/a/b/d", "path": "/a/b/e" }
]

这篇关于Web API 2-实施PATCH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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