简体   繁体   中英

ASP.NET MVC Core controller PATCH method for a Web API

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?
}

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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