ASP.NET MVC Core controller PATCH method for a Web

2019-08-03 14:29发布

Given a database table Person holding the 3 colums Id, Firstname and Lastname.

How does the ASP.NET Core Web API MVC controller methods PATCH (modify only the lastname) look like when using a real DbContext.

I do not know how to implement it at all (and can't find a tutorial on that) so I ended up with the code below but do not know how to procede.

[HttpPatch("{id}")]
public void Patch(int id, [FromBody]Person value) 
{
  //is this the correct method signature?
  // what to do with dbContext in here?
}

2条回答
孤傲高冷的网名
2楼-- · 2019-08-03 14:50

If you have already implemented the client logic you can use SimplePatch (how to use in the link).

Disclaimer: I'm the author of the project.

查看更多
走好不送
3楼-- · 2019-08-03 14:56

So, PATCH-request, accordingly to the REST guidelines should provide the ability to partially modify the resource with only properties and values given in request. If you are confused about how to project partially changes on your DB - there is a simple answer. Update only those DB-fields, which are affected in PATCH request :).

There are some libraries, available in NuGet, which can help you to implement e PATCH in your API.Take a look at JsonPatch for example. There is a good sample from controller till DB.

UPD: Another one library

查看更多
登录 后发表回答