在Asp.Net Core 3.0中如何使用 Newtonsoft.Json 库序列化数据

在.Net Core 3.0中 内置了一套Json序列化/反序列化方案,默认可以不再依赖,不再支持   Newtonsoft.Json.

但是.NET Core 3.0 System.Text.Json 和 Newtonsoft.Json 使用方法不一致,对于3.0以前版本升级有限制。如果前端代码以固定更没法用了。

在Asp.Net Core 3.0中如何使用  Newtonsoft.Json 库序列化数据

官方给出了兼容处理方案,操作步骤如下:

1.引用Microsoft.AspNetCore.Mvc.NewtonsoftJson 库

Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson -Version 3.0.0

2.在服务配置中添加 支持使用

// 配置服务
public void ConfigureServices(IServiceCollection services)
{
    //配置Mvc + json 序列化
    services.AddMvc(options => { options.EnableEndpointRouting = false; })
            .SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
                .AddNewtonsoftJson(options =>
                {
                    options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
                    options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm";
                });
}

使用方式和序列方式和 以前一样了。

更多:

Asp.Net Core Cookie使用,Asp.net Core Cookie操作失效

asp.net core session使用

Asp.net Core CacheHelper 通用缓存帮助类

原文地址:https://www.cnblogs.com/tianma3798/p/11909245.html

时间: 2024-07-29 04:10:46

在Asp.Net Core 3.0中如何使用 Newtonsoft.Json 库序列化数据的相关文章

在ASP.NET Core 2.0中使用CookieAuthentication

在ASP.NET Core中关于Security有两个容易混淆的概念一个是Authentication(认证),一个是Authorization(授权).而前者是确定用户是谁的过程,后者是围绕着他们允许做什么,今天的主题就是关于在ASP.NET Core 2.0中如何使用CookieAuthentication认证. 在ASP.NET Core 2.0中使用CookieAuthentication跟在1.0中有些不同,需要在ConfigureServices和Configure中分别设置,前者我

说说ASP.Net Core 2.0中的Razor Page

随着.net core2.0的发布,我们可以创建2.0的web应用了.2.0中新东西的出现,会让我们忘记老的东西,他就是Razor Page.下面的这篇博客将会介绍ASP.Net Core 2.0中的Razor Page. 在ASP.Net Core 2.0新特点之一就是支持Razor Page.今天的Razor Page是ASP.Net Core MVC中的一个子集.ASP.Net Core MVC 支持Razor Page意味着Razor Page应用从技术上来说就是MVC应用,同时Razo

用ASP.NET Core 1.0中实现邮件发送功能

准备将一些项目迁移到 asp.net core 先从封装类库入手,在遇到邮件发送类时发现在 asp.net core 1.0中并示提供SMTP相关类库,于是网上一搜发现了MailKit 好东西一定要试一下,何况是开源,下面是代码可实现SMTP邮件发送: using MailKit.Net.Smtp; using MailKit.Security; using MimeKit; using System.Threading.Tasks; namespace ConsoleApp1 { public

ASP.NET Core 1.0 中使用 Swagger 生成文档

github:https://github.com/domaindrivendev/Ahoy 之前文章有介绍在ASP.NET WebAPI 中使用Swagger生成文档,ASP.NET Core 1.0中同样也支持. 依赖包 "dependencies": { "Swashbuckle.SwaggerGen": "6.0.0-rc1-final", "Swashbuckle.SwaggerUi": "6.0.0-rc

ASP.NET Core 2.0中的HttpContext

ASP.NET Core 2.0中的HttpContext https://blog.csdn.net/weixin_34174322/article/details/87012345 将 Net 项目升级 Core项目经验:(二)修复迁移后Net Standard项目中的错误 https://www.colabug.com/2742683.html NET Core中怎么使用HttpContext.Current https://www.cnblogs.com/Leo_wl/p/6195683

避免在ASP.NET Core 3.0中为启动类注入服务

本篇是如何升级到ASP.NET Core 3.0系列文章的第二篇. Part 1 - 将.NET Standard 2.0类库转换为.NET Core 3.0类库 Part 2 - IHostingEnvironment VS IHostEnvironent - .NET Core 3.0中的废弃类型 Part 3 - 避免在ASP.NET Core 3.0中为启动类注入服务(本篇) Part 4 - 将终端中间件转换为ASP.NET Core 3.0中的端点路由 Part 5 - 将集成测试的

【转】ASP.NET Core 2.0中的HttpContext

  ASP.NET Core 2.0中的HttpContext相较于ASP.NET Framework有一些变化,这边列出一些之间的区别.   在ASP.NET Framework中的 System.Web.HttpContext 对应 ASP.NET Core 2.0中的 Microsoft.AspNetCore.Http.HttpContext. HttpContext   HttpContext.Items转换成: IDictionary<object, object> items =

在C#中通过使用Newtonsoft.Json库来解析百度地图地理编码(GeoCoder)服务接口返回的Json格式的数据

百度地图地理编码(GeoCoder)服务接口返回的Json格式的数据,如下所示: http://api.map.baidu.com/geocoding/v3/?address=**省**市**区**路**号院**社区&output=json&ak=您的AK密钥 返回结果实例: { "status":0, "result": { "location":{"lng":116.79, "lat":

asp.net core 3.0中webapi post请求返回http 400

在Asp.net core 3.0的webapi项目中,发送json格式的post请求后,返回的header中error提示The JSON value could not be converted to 解决方法: 安装Microsoft.AspNetCore.Mvc.NewtonsoftJson 包 在ConfigureServices中添加services.AddNewtonsoftJson(); 原文地址:https://www.cnblogs.com/xbzhu/p/12104959.