JsonPatchDocument is null after migration to .Net Core 3

让人想犯罪 __ 提交于 2020-04-18 06:48:27

问题


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();

This is my Action:

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

This is the body-content:

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

Does anyone have an idea what is goung wrong here?


回答1:


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.

According to https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.0&tabs=visual-studio#jsonnet-support you should:

  1. Add a package reference to Microsoft.AspNetCore.Mvc.NewtonsoftJson

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

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

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



来源:https://stackoverflow.com/questions/58542164/jsonpatchdocument-is-null-after-migration-to-net-core-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!