简体   繁体   English

如何在ASP.NET Core 2.0 Web API中配置和使用Microsoft.AspNetCore.JsonPatch

[英]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.

相关问题 如何在asp.net core Web API startup class中配置JWT认证和Microsoft Authentication - how to configure JWT authentication and Microsoft Authentication in asp.net core Web API startup class ASP.NET Core 中的 SnakeCaseNamingStrategy 和 JsonPatch - SnakeCaseNamingStrategy and JsonPatch in ASP.NET Core 如何在ASP.NET Core中创建Microsoft GraphServiceClient Web Api 6 - How to create Microsoft GraphServiceClient in ASP.NET Core Web Api 6 可以在没有Microsoft.AspNetCore.All包的情况下创建ASP.NET Core 2.0项目吗? - Can an ASP.NET Core 2.0 project be created without a Microsoft.AspNetCore.All package? “ValidationModelActionFilterAttribute”类型必须派生自“Microsoft.AspNetCore.Mvc.Filters.IFilterMetadata”。 在 asp.net 核心 WEB API - The type 'ValidationModelActionFilterAttribute' must derive from 'Microsoft.AspNetCore.Mvc.Filters.IFilterMetadata'.' IN asp.net core WEB API ASP.NET Web API 2.0 管道和 ASP.NET Core Web API 管道的区别 - ASP.NET Web API 2.0 pipeline and ASP.NET Core Web API pipeline differences 如何在ASP.NET Core动态Web API中使用HttpGet? - How to use HttpGet in ASP.NET Core dynamic web api? ASP.NET Core 2.0 Web API IIS 托管和调试 - ASP.NET Core 2.0 Web API IIS Hosting and Debugging 在 ASP.NET Core 2.0 Web Api 中返回“原始”json - Return “raw” json in ASP.NET Core 2.0 Web Api ASP.NET Core 2.0 Web Api错误 - ASP.NET Core 2.0 Web Api Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM