迁移到 .Net Core 3 后 JsonPatchDocument 为空 [英] JsonPatchDocument is null after migration to .Net Core 3
问题描述
我有一个带有多个补丁操作的 AspNetCore-WebApi-Project,它在 Core 2.2 中运行良好.迁移到 Core 3 后,[FromBody] JsonPatchDocument
为空.我的 Get/Post 方法仍然按预期运行.
这是我创业的一部分:
services.AddDbContext(options => options.UseLazyLoadingProxies().UseNpgsql(Configuration.GetConnectionString("MyConnectionString"),选择 =>opt.UseNodaTime()));services.AddSwaggerGen(c =>{c.SwaggerDoc("v1", new OpenApiInfo { Title = "My-API", Version = "v1" });});services.AddControllers().AddNewtonsoftJson();
这是我的操作:
[HttpPatch("{id}")]公共异步任务补丁(Guid id,[FromBody] JsonPatchDocument补丁文件){等待 this.service.HandlePatchAsync(id, patchDocument);返回 NoContent();}
这是正文内容:
<预><代码>[{"op": "替换",路径":/名称",值":新名称"},{"op": "替换","路径": "/国家","value": "德国"}]有人知道这里出了什么问题吗?
我遇到了类似的问题.我打算完全摆脱 Newtonsoft
,但在那种情况下,带有 JsonPatchDocument
的补丁不起作用.
添加对
Microsoft.AspNetCore.Mvc.NewtonsoftJson
的包引用在启动时更改代码,将 MVC 添加到
services.AddMvc().AddNewtonsoftJson();
你做了第二步,但第一步呢?这对我有帮助.
不幸的是,我不知道如何在没有 .AddNewtonsoftJson()
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?
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:
Add a package reference to
Microsoft.AspNetCore.Mvc.NewtonsoftJson
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()
这篇关于迁移到 .Net Core 3 后 JsonPatchDocument 为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!