JsonPatchDocument为null

JsonPatchDocument为null

本文介绍了迁移到.Net Core 3后,JsonPatchDocument为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a AspNetCore-WebApi-Project with several patch-operations, which worked fine with Core 2.2. After migration to Core 3 the [FromBody] JsonPatchDocument<T> is null. My Get/Post-Methods are still functioning as expected.

这是我的创业公司的一部分:

This is one part of my Startup:

    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"
    }
]

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

Does anyone have an idea what is goung wrong here?

推荐答案

我遇到了类似的问题.我将完全摆脱Newtonsoft,但是在那种情况下,带有JsonPatchDocument的补丁无法正常工作.

I struggle with a similar issue. I was going to get rid of Newtonsoft at all, but in that case the patch with JsonPatchDocument was not working.

根据 https://docs.microsoft.com/zh-cn/aspnet/core/migration/22-to-30?view=aspnetcore-3.0&tabs=visual-studio#jsonnet-支持,您应该:

  1. 添加对Microsoft.AspNetCore.Mvc.NewtonsoftJson

在启动时更改代码,将MVC添加到services.AddMvc().AddNewtonsoftJson();

Change code in the startup adding MVC to services.AddMvc().AddNewtonsoftJson();

您做了第二步,但是第一步呢?这对我有帮助.

You did the second step, but what about the first?This helped me.

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

Unfortunatelly, I do not know how to make JsonPatchDocument work without .AddNewtonsoftJson()

这篇关于迁移到.Net Core 3后,JsonPatchDocument为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 18:22