在.Net Core 3.0中 內置了一套Json序列化/反序列化方案,默認可以不再依賴,不再支持 Newtonsoft.Json.
但是.NET Core 3.0 System.Text.Json 和 Newtonsoft.Json 使用方法不一致,對於3.0以前版本升級有限制。如果前端代碼以固定更沒法用了。
在Asp.Net Core 3.0中如何使用 Newtonsoft.Json 庫序列化數據
官方給出了兼容處理方案,操作步驟如下:
1.引用Microsoft.AspNetCore.Mvc.NewtonsoftJson 庫
Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson -Version 3.0.0
2.在服務配置中添加 支持使用
// 配置服務 public void ConfigureServices(IServiceCollection services) { //配置Mvc + json 序列化 services.AddMvc(options => { options.EnableEndpointRouting = false; }) .SetCompatibilityVersion(CompatibilityVersion.Version_3_0) .AddNewtonsoftJson(options => { options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm"; }); }
使用方式和序列方式和 以前一樣了。
更多: