為什么要引用NewtonsoftJson
.net5 內置了一套Json序列化/反序列化方案,默認可以不再依賴,不再支持 Newtonsoft.Json.
.net5 System.Text.Json 和 Newtonsoft.Json 使用方法不一致,以及其他的差役建議使用Newtonsoft.Json
NewtonsoftJson
引用庫:
Microsoft.AspNetCore.Mvc.NewtonsoftJson
注冊服務:

services.AddControllers().AddNewtonsoftJson(options =>
{
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
});
關於時間格式的處理
如果顯示的日期是“yyyy-MM-dd”的格式,可以在顯示的Dto類中添加一個字段,規定顯示的格式,比如:
using Supernode.Hr.Common;
using Supernode.Hr.Domain.Model.Enum;
using System;
namespace Supernode.Hr.Application.Attendance.Dto
{
public class AttendanceShowDto
{
/// <summary>
/// 考勤日期
/// </summary>
public DateTime AttendanceDate { get; set; }
/// <summary>
/// 考勤日期[string]
/// </summary>
public string AttendanceDateString => AttendanceDate.ToString("yyyy-MM-dd");
}
}
