Web API的ASP.NET MVC核心控制器PATCH方法 [英] ASP.NET MVC Core controller PATCH method for a Web API

查看:52
本文介绍了Web API的ASP.NET MVC核心控制器PATCH方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个数据库表,其中包含3个列ID,名字和姓氏的人.

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

使用真实的DbContext时,ASP.NET Core Web API MVC控制器方法PATCH(仅修改姓氏)的样子.

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

推荐答案

因此,根据REST准则,PATCH请求应该提供仅使用请求中给定的属性和值来部分修改资源的能力.如果您对如何在数据库上部分投影更改感到困惑-有一个简单的答案.仅更新那些在PATCH请求中受影响的数据库字段:).

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 :).

NuGet中提供了一些库,这些库可以帮助您在API中实现e PATCH.看看 JsonPatch .从控制器到DB都有一个很好的样本.

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:另一个图书馆

这篇关于Web API的ASP.NET MVC核心控制器PATCH方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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