[英]How to configure and use Microsoft.AspNetCore.JsonPatch in ASP.NET Core 2.0 Web API
I am creating an HTTP Partial method in my ASP.NET Web API controller and I read this document http://benfoster.io/blog/aspnet-core-json-patch-partial-api-updates on how to achieve HTTP Partial methods in a controller. 我正在ASP.NET Web API控制器中创建HTTP Partial方法,并阅读了有关如何实现HTTP Partial方法的文档http://benfoster.io/blog/aspnet-core-json-patch-partial-api-updates在控制器中。 I get an exception when I hit the HTTP Partial endpoint that says 当我点击HTTP Partial端点时出现异常
Here is my code for the Patch method in the controller: 这是我在控制器中的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);
}
And here is the JSON body I'm sending through postman in the HTTP PATCH request: 这是我通过HTTP PATCH请求中的邮递员发送的JSON正文:
I feel like I need to do something in the Startup.cs file such as configuring the JsonPatchDocument but I don't know how. 我觉得我需要在Startup.cs文件中做一些事情,例如配置JsonPatchDocument,但我不知道如何做。 Any help is much appreciated. 任何帮助深表感谢。
I think i found your issue: "Note that we always send an array of operations even if you're only sending a single operation." 我想我发现了您的问题:“请注意,即使您仅发送单个操作,我们也总是发送一系列操作。”
Try to change your request in: 尝试在以下位置更改您的请求:
[
{
"op": "replace",
"path": "/email",
"value": "THIS_SOME_OTHER_EMAIL@gmail.com"
}
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.