Json asp.net core 3.1 upgrade to encounter abnormal

Json exceptions encountered:
Severity Item Code prohibited document display state line
access Extension Method error CS1061 ' "JsonOptions" is not included in the definition of "SerializerSettings", and could not find a first acceptable "JsonOptions" type parameter " SerializerSettings "(are you missing a using directive or an assembly reference?)

Approach:

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

change into:

            services.AddMvc().AddNewtonsoftJson(options =>
            {
                //忽略循环引用
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                //不使用驼峰样式的key
                options.SerializerSettings.ContractResolver = new DefaultContractResolver();             
            });

Access to information:
https://docs.microsoft.com/en-us/aspnet/core/release-notes/aspnetcore-3.0?view=aspnetcore-3.1

Which said:

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.

Find the solution in this article
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.

As part of ASP.NET Core 3.0, which by default is no longer the team include Json.NET. You can learn more about this in the announcement about making significant changes to Microsoft.AspNetCore.App in.

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" .
Instead Json.NET, ASP.NET Core 3.0 .NET Core 3.0 and comprising a different JSON API, the API is more emphasis on performance. You can learn more information about the bulletin ".NET Core 3.0 JSON in the future" in.

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 of the new template will no longer be bundled with Json.NET, but you can easily re-configure the project to use it instead of the new JSON library. This is important for compatibility with earlier projects and should not completely replace the new library, so you can not see all the features here.

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:

In order to use Json.NET reconfigure ASP.NET Core 3.0 project, you will need to add a reference to Microsoft.AspNetCore.Mvc.NewtonsoftJson of NuGet, the package contains all the necessary bits. Then, you need to configure MVC startup company ConfigureServices, as follows:

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.

This will set the MVC controller and configure it to use Json.NET instead of the new API. In addition to the controller, you can also use other MVC overloaded (for example, the controller has a page view or Razor). The AddNewtonsoftJson method has an overload that allows you to use AddJsonOptions like in ASP.NET Core 2.x Json.NET the same configuration options.

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

It seems to be taking the time to compare 3.1 system.text.json certification and newton.json better spent.

Guess you like

Origin www.cnblogs.com/Wadereye/p/12298091.html