如何处理ASP.NET MVC5 返回JsonResult DateTime类型格式化问题

1、问题

ASP.NET MVC在返回JSON对象的时候,DateTime类型默认返回的是:/Date("xxxxxxxxxxxx")/
如何处理ASP.NET MVC5 返回JsonResult DateTime类型格式化问题_第1张图片

2、解决

使用Newtonsoft.Json 对日期进行格式化,首先在项目中安装Newtonsoft.Json

2.1、自定义一个JsonResult的子类

重写JsonResultExecuteResult方法

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace JNET.Web.Infrastructure.Convert
{
    public class JsonNetResult: JsonResult
    {
        /// 
        /// 日期格式
        /// 
        public string DateFormatStr { get; set; } = "yyyy-MM-dd HH:mm:ss";
        public JsonNetResult()
        {
        }
        public JsonNetResult(object data)
        {

        }
        public override void ExecuteResult(ControllerContext context)
        {
            HttpResponseBase response = context.HttpContext.Response;
            if (string.IsNullOrEmpty(this.ContentType))
            {
                response.ContentType = "application/json";
            }
            else
            {
                response.ContentType = this.ContentType;
            }
            if (ContentEncoding != null)
                response.ContentEncoding = ContentEncoding;
            if (Data != null)
            {
                JsonTextWriter writer = new JsonTextWriter(response.Output) { Formatting = Formatting.Indented, DateFormatString=DateFormatStr };
                JsonSerializer serializer = JsonSerializer.Create(new JsonSerializerSettings());
                serializer.Serialize(writer, Data);
                writer.Flush();
            }
        }
    }
}

2.1、定义BaseController

定义BaseController重写JsonResult方法

BaseController代码:

public class BaseController: Controller
    {
     
          /// 
        /// 重写JsonResult
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding, JsonRequestBehavior behavior)
        {
            return new JsonNetResult
            {
                Data = data,
                ContentType = contentType,
                ContentEncoding = contentEncoding,
                JsonRequestBehavior = behavior,
                DateFormatStr= "yyyy-MM-dd HH:mm:ss"
            };
        }
        /// 
        /// 自定义JSON返回
        /// 
        /// 
        /// 
        /// 
        /// 
        protected JsonResult ResultJson(object data, JsonRequestBehavior behavior, string DateFormatStr)
        {
            return new JsonNetResult
            {
                Data = data,
                JsonRequestBehavior = behavior,
                DateFormatStr= DateFormatStr
            };
        }
        /// 
        /// 自定义JSON返回
        /// 
        /// 
        /// 
        /// 
        protected JsonResult ResultJson(object data, JsonRequestBehavior behavior)
        {
            return new JsonNetResult
            {
                Data = data,
                JsonRequestBehavior = behavior
            };
        }
        /// 
        /// 自定义JSON返回
        /// 
        /// 
        /// 
        /// 
        protected JsonResult ResultJson(object data, string DateFormatStr)
        {
            return new JsonNetResult
            {
                Data = data,
                DateFormatStr = DateFormatStr
            };
        }
        /// 
        /// 自定义JSON返回
        /// 
        /// 
        /// 
        protected JsonResult ResultJson(object data)
        {
            return new JsonNetResult
            {
                Data = data
            };
        }
    }

3、使用

在对应的Controller中继承BaseController

       /// 
        /// 返回列表数据
        /// 
        /// 
        public JsonResult ListData()
        {
            List<Office> list = _officeService.GetAllEntity();
            return this.ResultJson(list, JsonRequestBehavior.AllowGet);
        }

你可能感兴趣的:(ASP.NET,MVC)