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


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

處理方法:

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

修改為:

            services.AddMvc().AddNewtonsoftJson(options =>
            {
                //忽略循環引用
                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。您可以在有關對Microsoft.AspNetCore.App進行重大更改的公告中了解有關此內容的更多信息。

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更加注重性能。您可以在有關“ .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庫。這對於與較早項目的兼容性以及對新庫都不應完全替代都非常重要,因此您在此處看不到全部功能。

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.

這將設置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認證更好用了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM