asp.net core 3.0 MVC JSON 全局配置
2021-05-11 12:28
标签:循环引用 creat netcore net 输出 lin trail resolve lse asp.net core 3.0 MVC JSON 全局配置 标签:循环引用 creat netcore net 输出 lin trail resolve lse 原文地址:https://www.cnblogs.com/lenovo_tiger_love/p/12054253.htmlasp.net core 3.0 MVC JSON 全局配置
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;
public void ConfigureServices(IServiceCollection services)
{
services.AddJsonOptions(options =>
{
//格式化日期时间格式
options.JsonSerializerOptions.Converters.Add(new DatetimeJsonConverter());
//数据格式首字母小写
//options.JsonSerializerOptions.PropertyNamingPolicy =JsonNamingPolicy.CamelCase;
//数据格式原样输出
options.JsonSerializerOptions.PropertyNamingPolicy = null;
//取消Unicode编码
options.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All);
//忽略空值
options.JsonSerializerOptions.IgnoreNullValues = true;
//允许额外符号
options.JsonSerializerOptions.AllowTrailingCommas = true;
//反序列化过程中属性名称是否使用不区分大小写的比较
options.JsonSerializerOptions.PropertyNameCaseInsensitive = false;
});
}
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
public class DatetimeJsonConverter : JsonConverter
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
public void ConfigureServices(IServiceCollection services)
{
services.AddNewtonsoftJson(options =>
{
//设置时间格式
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
//忽略循环引用
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
//数据格式首字母小写
//options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
//数据格式按原样输出
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
//忽略空值
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
});
}
下一篇:JS开发——文件夹的上传和下载
文章标题:asp.net core 3.0 MVC JSON 全局配置
文章链接:http://soscw.com/index.php/essay/84279.html