.net5 - 創建Web.Api項目(三)NewtonsoftJson的引用 及 時間格式的處理


為什么要引用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");
    }
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM