升級到asp.net core 3.1遇到的json異常

遇到的json異常:
嚴重性 代碼 說明 項目 文件 行 禁止顯示狀態(tài)
錯誤 CS1061 '“JsonOptions”未包含“SerializerSettings”的定義,并且找不到可接受第一個“JsonOptions”類型參數(shù)的可訪問擴展方法“SerializerSettings”(是否缺少 using 指令或程序集引用?)

處理方法:

services.AddMvc().AddJsonOptions(options =>
            {
                //忽略循環(huán)引用
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                //不使用駝峰樣式的key
                options.SerializerSettings.ContractResolver = new DefaultContractResolver();               
            });

修改為:

            services.AddMvc().AddNewtonsoftJson(options =>
            {
                //忽略循環(huán)引用
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                //不使用駝峰樣式的key
                options.SerializerSettings.ContractResolver = new DefaultContractResolver();             
            });

查閱資料:
https://docs.microsoft.com/en-us/aspnet/core/release-notes/aspnetcore-3.0?view=aspnetcore-3.1

里面說:

New JSON serialization

ASP.NET Core 3.0 now uses System.Text.Json by default for JSON serialization:

  • Reads and writes JSON asynchronously.
  • Is optimized for UTF-8 text.
  • Typically higher performance than Newtonsoft.Json.

To add Json.NET to ASP.NET Core 3.0, see Add Newtonsoft.Json-based JSON format support.

在這篇文章里查找到解決辦法
https://stackoverflow.com/questions/55666826/where-did-imvcbuilder-addjsonoptions-go-in-net-core-3-0

簡單翻譯如下:
As part of ASP.NET Core 3.0, the team moved away from including Json.NET by default. You can read more about that in general in the announcement on breaking changes to Microsoft.AspNetCore.App.

作為ASP.NET Core 3.0的一部分,該團隊默認情況下不再包括Json.NET。您可以在有關(guān)對Microsoft.AspNetCore.App進行重大更改的公告中了解有關(guān)此內(nèi)容的更多信息。

Instead of Json.NET, ASP.NET Core 3.0 and .NET Core 3.0 include a different JSON API that focuses a bit more on performance. You can learn about that more in the announcement about “The future of JSON in .NET Core 3.0”.
代替Json.NET,ASP.NET Core 3.0和.NET Core 3.0包括一個不同的JSON API,該API更加注重性能。您可以在有關(guān)“ .NET Core 3.0中JSON的未來”的公告中了解更多信息。

The new templates for ASP.NET Core will no longer bundle with Json.NET but you can easily reconfigure the project to use it instead of the new JSON library. This is important for both compatibility with older projects and also because the new library is not supposed to be a full replacement, so you won't see the full feature set there.

ASP.NET Core的新模板將不再與Json.NET捆綁在一起,但是您可以輕松地重新配置項目以使用它而不是新的JSON庫。這對于與較早項目的兼容性以及對新庫都不應(yīng)完全替代都非常重要,因此您在此處看不到全部功能。

In order to reconfigure your ASP.NET Core 3.0 project with Json.NET, you will need to add a NuGet reference to Microsoft.AspNetCore.Mvc.NewtonsoftJson, which is the package that includes all the necessary bits. Then, in the Startup’s ConfigureServices, you will need to configure MVC like this:

為了使用Json.NET重新配置ASP.NET Core 3.0項目,您將需要添加對Microsoft.AspNetCore.Mvc.NewtonsoftJson的NuGet引用,該包包含所有必要的位。然后,您需要在啟動公司的ConfigureServices中配置MVC,如下所示:

services.AddControllers()
    .AddNewtonsoftJson();

This sets up MVC controllers and configures it to use Json.NET instead of that new API. Instead of controllers, you can also use a different MVC overload (e.g. for controllers with views, or Razor pages). That AddNewtonsoftJson method has an overload that allows you to configure the Json.NET options like you were used to with AddJsonOptions in ASP.NET Core 2.x.

這將設(shè)置MVC控制器并將其配置為使用Json.NET而不是該新API。除了控制器以外,您還可以使用其他MVC重載(例如,具有視圖或Razor頁面的控制器)。該AddNewtonsoftJson方法有一個重載,使您可以像在ASP.NET Core 2.x中使用AddJsonOptions一樣配置Json.NET選項。

services.AddControllers()
    .AddNewtonsoftJson(options =>
    {
        options.SerializerSettings.ContractResolver = new DefaultContractResolver();
    });

看來要抽空對比一下3.1的system.text.json和newton.json認證更好用了。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 趁著武漢疫情,在家研究原來2.2的框架升級到3.1的問題,在過程中遇到不少坑,好在放假有的是時間,一個一個解決,現(xiàn)...
    跋涉者129閱讀 2,258評論 0 0
  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom閱讀 3,184評論 0 3
  • 文/綠駿馬 遇見一位老戰(zhàn)友,他轉(zhuǎn)業(yè)在民政局,對自己當(dāng)前的工作很滿意,雖然每天忙忙碌碌,但很充實。我問他:“充實指什...
    綠駿馬sja閱讀 486評論 0 2
  • 安裝不成功: 好像因為已經(jīng)安裝了,所以報錯,卸載 刪掉再重新安裝3x的版本就可以了
    小囧兔閱讀 192評論 0 0
  • 黑是夜 黑是冷酷 黑是嫉妒 黑是我
    是戶一閱讀 109評論 0 0

友情鏈接更多精彩內(nèi)容