为什么要引用NewtonsoftJson

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

.net5 System.Text.Json 和 Newtonsoft.Json 使用方法不一致,以及其他的差役建议使用Newtonsoft.Json

NewtonsoftJson

 引用库:

Microsoft.AspNetCore.Mvc.NewtonsoftJson

 注册服务:

.net5 - 创建Web.Api项目(三)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");
    }
}

  

相关文章:

  • 2021-11-05
  • 2021-09-29
  • 2022-12-23
  • 2021-08-13
  • 2022-12-23
  • 2022-12-23
  • 2021-09-03
  • 2022-12-23
猜你喜欢
  • 2022-02-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案