使用Newtonsoft.json 解决 Asp.Net MVC DateTime类型数据Json格式化问题

时间:2024-01-10 15:08:32

解决思路

众所周知,MVC中调用的微软的组件JavaScriptSerialer...,格式DateTime类型数据需要在客户端专门解。

还知道,NewtonSoft.json可以“正确”的格式化DateTime类型的数据。

但是,如果在MVC中使用NewtonSoft.json的话,则需要调用Controller.Content(),返回的为字符串,客户端还要做转换。

而,Action返回的结果都是JsonResult。

于是,我用NewtonSoft.json的方法封装了一个NewtonJsonReuslt的类型,供Controller调用,具体的参考的MVC JsonResult的源码,没有技术含量。

代码部分

using System.Web.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters; namespace System.Web.Mvc
{
public class NewtonJsonResult : JsonResult
{
public JsonSerializerSettings JsonSerializerSettings { get; set; }
public NewtonJsonResult()
{
this.JsonRequestBehavior = JsonRequestBehavior.DenyGet;
}
public NewtonJsonResult(object obj)
{
this.JsonRequestBehavior = JsonRequestBehavior.DenyGet;
this.Data = obj;
}
public NewtonJsonResult(object obj,JsonSerializerSettings jsonSerializerSettings)
{
this.JsonRequestBehavior = JsonRequestBehavior.DenyGet;
this.Data = obj;
this.JsonSerializerSettings = jsonSerializerSettings;
} public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
if ((this.JsonRequestBehavior == JsonRequestBehavior.DenyGet) && (string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase)))
{
throw new InvalidOperationException("改方法当前不允许使用Get");
}
HttpResponseBase response = context.HttpContext.Response;
if (!string.IsNullOrEmpty(this.ContentType))
{
response.ContentType = this.ContentType;
}
else
{
response.ContentType = "application/json";
}
if (this.ContentEncoding != null)
{
response.ContentEncoding = this.ContentEncoding;
}
if (this.Data != null)
{
string strJson = JsonConvert.SerializeObject(this.Data, JsonSerializerSettings);
response.Write(strJson);
response.End();
}
}
}
}

查看代码

调用demo:

 public ActionResult Get(int pageIndex)
{
IArticleService service = new ArticleService();
int allPageCount;
List<Model.Article> list = service.GetArticleByPage(PAGESIZE, pageIndex, out allPageCount);
return new NewtonJsonResult(list, new JsonSerializerSettings() { DateFormatString="yyyy年MM月dd日 HH:mm:ss" });
}

查看代码

上面代码随手写的,有错误,望大家勿喷...

另外,还写了静态的ControllBase扩展方法

using System.Text;
using System.Web.Mvc; namespace System.Web.Mvc
{
public static class ControllerExtend
{
public static ActionResult NewtonJson(this ControllerBase controller, object obj)
{
return NewtonJson(controller, null, null, JsonRequestBehavior.DenyGet, obj);
}
public static ActionResult NewtonJson(this ControllerBase controller, Encoding encoding, string contentType, JsonRequestBehavior jsonRequestBehavior, object obj)
{
return new NewtonJsonResult() { ContentEncoding = encoding, ContentType = contentType, JsonRequestBehavior = jsonRequestBehavior, Data = obj };
}
public static ActionResult NewtonJson(this ControllerBase controller, object obj, JsonRequestBehavior jsonRequestBehavior)
{
return NewtonJson(controller, null, null, jsonRequestBehavior, obj);
}
public static ActionResult NewtonJson(this ControllerBase controller, object obj, Encoding encoding, JsonRequestBehavior jsonRequestBehavior)
{
return NewtonJson(controller, encoding, null, jsonRequestBehavior, obj);
}
public static ActionResult NewtonJson(this ControllerBase controller, object obj, string contentType)
{
return NewtonJson(controller, null, contentType, JsonRequestBehavior.DenyGet, obj);
}
public static ActionResult NewtonJson(this ControllerBase controller, object obj, Encoding encoding)
{
return NewtonJson(controller, encoding, null, JsonRequestBehavior.DenyGet, obj);
}
public static ActionResult NewtonJson(this ControllerBase controller, object obj, Encoding encoding, string contentType)
{
return NewtonJson(controller, encoding, contentType, JsonRequestBehavior.DenyGet, obj);
}
public static ActionResult NewtonJson(this ControllerBase controller, object obj, Encoding encoding, string contentType, JsonRequestBehavior jsonRequestBehavior)
{
return NewtonJson(controller, encoding, contentType, jsonRequestBehavior, obj);
}
}
}

查看代码

不过,调用的时候有点小问题,看下面的最后两个return语句,不太明白为什么扩展方法必须要写一个对象去调用?难道是扩展方法中的第一个参数导致的必须写?还望路过的大侠给小弟解惑,小弟不胜感激...

 public ActionResult Get(int pageIndex)
{
IArticleService service = new ArticleService();
int allPageCount;
List<Model.Article> list = service.GetArticleByPage(PAGESIZE, pageIndex, out allPageCount);
return new NewtonJsonResult(list, new JsonSerializerSettings() { DateFormatString="yyyy年MM月dd日 HH:mm:ss" });
return NewtonJson(list);//这样是直接报错的,找不到方法,必须按照下面的写法才行..
return this.NewtonJson(list);
}