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

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 趁着武汉疫情,在家研究原来2.2的框架升级到3.1的问题,在过程中遇到不少坑,好在放假有的是时间,一个一个解决,现...
    跋涉者129阅读 6,537评论 0 0
  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 7,905评论 0 3
  • 文/绿骏马 遇见一位老战友,他转业在民政局,对自己当前的工作很满意,虽然每天忙忙碌碌,但很充实。我问他:“充实指什...
    绿骏马sja阅读 3,229评论 0 2
  • 安装不成功: 好像因为已经安装了,所以报错,卸载 删掉再重新安装3x的版本就可以了
    小囧兔阅读 1,396评论 0 0
  • 黑是夜 黑是冷酷 黑是嫉妒 黑是我
    是户一阅读 1,013评论 0 0

友情链接更多精彩内容