【发布时间】:2018-05-02 04:46:33
【问题描述】:
我正在我的 ASP.NET Web API 控制器中创建一个 HTTP Partial 方法,我阅读了这篇文档http://benfoster.io/blog/aspnet-core-json-patch-partial-api-updates,了解如何在控制器中实现 HTTP Partial 方法。当我点击显示 的 HTTP 部分端点时出现异常
这是我在控制器中的 Patch 方法的代码:
[HttpPatch("{userId}")]
public IActionResult Patch([FromRoute(Name = "userId")]Guid userId, [FromBody] JsonPatchDocument<User> userProperties)
{
var indexOfUserToPartiallyUpdate = UsersInMemory.List.FindIndex(user => user.Id == userId);
if (indexOfUserToPartiallyUpdate == -1)
{
return BadRequest($"user with {userId} not found.");
}
var originalUser = UsersInMemory.List[indexOfUserToPartiallyUpdate];
userProperties.ApplyTo(UsersInMemory.List[indexOfUserToPartiallyUpdate], ModelState);
if (!ModelState.IsValid)
{
return new BadRequestObjectResult(ModelState);
}
var model = new
{
beforePatch = originalUser,
afterPatch = UsersInMemory.List[indexOfUserToPartiallyUpdate]
};
return Ok(model);
}
这是我在 HTTP PATCH 请求中通过邮递员发送的 JSON 正文:
我觉得我需要在 Startup.cs 文件中做一些事情,例如配置 JsonPatchDocument,但我不知道该怎么做。非常感谢任何帮助。
【问题讨论】: