迁移到 .Net Core 3 后 JsonPatchDocument 为空

JsonPatchDocument is null after migration to .Net Core 3

我有一个带有多个补丁操作的 AspNetCore-WebApi-Project,它在 Core 2.2 上运行良好。迁移到 Core 3 后,[FromBody] JsonPatchDocument<T> 为空。我的 Get/Post-Methods 仍在按预期运行。

这是我创业的一部分:

    services.AddDbContext<MyContext>(options => options
                    .UseLazyLoadingProxies()
                    .UseNpgsql(Configuration.GetConnectionString("MyConnectionString"), 
                        opt => opt.UseNodaTime()));

    services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new OpenApiInfo { Title = "My-API", Version = "v1" });
    });
    services.AddControllers()
        .AddNewtonsoftJson();

这是我的操作:

[HttpPatch("{id}")]
public async Task<IActionResult> Patch(Guid id, 
                            [FromBody] JsonPatchDocument<MyViewModel> patchDocument)
{
    await this.service.HandlePatchAsync(id, patchDocument);
    return NoContent();
}

这是正文内容:

[   
    {
        "op": "replace",
        "path": "/name",
        "value": "New Name" 
    },
    {
        "op": "replace",
        "path": "/country",
        "value": "Germany" 
    }
]

有人知道这里出了什么问题吗?

我遇到了类似的问题。我打算完全摆脱 Newtonsoft,但在那种情况下,带有 JsonPatchDocument 的补丁不起作用。

根据 https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.0&tabs=visual-studio#jsonnet-support 你应该:

  1. 添加对 Microsoft.AspNetCore.Mvc.NewtonsoftJson 的包引用

  2. 将启动中的代码更改为 services.AddMvc().AddNewtonsoftJson();

你做了第二步,那么第一步呢? 这对我有帮助。

不幸的是,我不知道如何在没有 .AddNewtonsoftJson()

的情况下使 JsonPatchDocument 工作

从 .NET 5 开始,JsonPatchDocument 似乎依赖于来自 NewtonsoftJson 的 JsonSerializers。也就是说,在JsonPatchDocument的源码中,有

[JsonConverter(typeof(JsonPatchDocumentConverter))]
public class JsonPatchDocument : IJsonPatchDocument
{

其中 JsonConverter 属性来自 Newtonsoft Json 命名空间。

我想我可以定义一个 Json 转换器,以便它在 System.Text.Json 中工作。 像这样

using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.JsonPatch;
using Microsoft.AspNetCore.JsonPatch.Operations;
using Newtonsoft.Json.Serialization;

namespace MyProject
{
    public class JsonPatchDocumentConverter : JsonConverter<JsonPatchDocument>
    {
        public override JsonPatchDocument Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            var result = JsonSerializer.Deserialize<List<Operation>>(ref reader, options);
            return new JsonPatchDocument(result, new DefaultContractResolver());
        }

        public override void Write(Utf8JsonWriter writer, JsonPatchDocument value, JsonSerializerOptions options)
        {
            throw new NotImplementedException();
        }
    }
}

然后在Startup.cs,

        services.AddControllersWithViews().AddJsonOptions(options =>
        {
            options.JsonSerializerOptions.Converters.Add(new JsonPatchDocumentConverter());
        });

... 或者,继承 JsonPatchDocument 并在那里定义属性

namespace MyProject        
{
    [System.Text.Json.Serialization.JsonConverter(typeof(JsonPatchDocumentConverter))]
    public class JsonPatchDocument : Microsoft.AspNetCore.JsonPatch.JsonPatchDocument
    {

    }
}

然后使用 JsonPatchDocument 子类,以便 System.Text.Json

从属性中提取 Json 转换器
public async Task<IActionResult> Patch(Guid id, 
                            [FromBody] MyProject.JsonPatchDocument patchDocument)
{