今天在将一个 ASP.NET Core 项目从 .NET 6 升级到 .NET 9 遇到的问题
下面的代码中 CamelCasePropertyNamesContractResolver
来自 Newtonsoft.Json.Serialization
var patchDoc = new JsonPatchDocument<BannerAddDto>(
new List<Operation<BannerAddDto>>
{
new("replace", "/image", null, image),
},
new CamelCasePropertyNamesContractResolver());
如果换成 System.Text.Json
,该用哪个 IContractResolver
实现?
为什么非要换呢?
1.替换库:
Microsoft.AspNetCore.Mvc.NewtonsoftJson,替换:SystemTextJsonPatch
的确用 SystemTextJsonPatch 可以解决,SystemTextJsonPatch 是一个开源项目,需要专门安装这个包
dotnet add package SystemTextJsonPatch
命名空间
using Microsoft.AspNetCore.JsonPatch;
using Microsoft.AspNetCore.JsonPatch.Operations;
改成
using SystemTextJsonPatch;
using SystemTextJsonPatch.Operations;