升级到asp.net core 3.1遇到的json异常

2019/04/10 10:10
阅读数 170

遇到的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认证更好用了。

原文出处:https://www.cnblogs.com/Wadereye/p/12298091.html

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部