解决思路

众所周知,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);
}

使用Newtonsoft.json 解决 Asp.Net MVC DateTime类型数据Json格式化问题的更多相关文章

  1. 解决python中转化成json的方法不能序列化datetime类型数据(转)

    Python自带的json.dumps方法序列化数据时候如果格式化的数据中有datetime类型数据时候会提示错误TypeError: datetime.datetime(2012, 12, 12, ...

  2. JSON之Asp.net MVC C#对象转JSON,DataTable转JSON,List转JSON,JSON转List,JSON转C#对象

    一.JSON解析与字符串化 JSON.stringify() 序列化对象.数组或原始值 语法:JSON.stringify(o,filter,indent) o,要转换成JSON的对象.数组或原始值 ...

  3. JSON之Asp.net MVC C#对象转JSON,DataTable转JSON,List<T>转JSON,JSON转List<T>,JSON转C#对象

    一.JSON解析与字符串化 JSON.stringify() 序列化对象.数组或原始值 语法:JSON.stringify(o,filter,indent) o,要转换成JSON的对象.数组或原始值 ...

  4. 解决Win10系统下 C# DateTime 出现星期几的问题 解决ASP.NET MVC 接受Request Playload参数问题

    解决Win10系统下 C# DateTime 出现星期几的问题 昨天晚上写代码的时候偶然发现 DateTime 里出现了星期几,当时一阵凌乱,去网上百度没有详细解决办法,很多人说可以用用 ToStri ...

  5. 自定义JsonResult处理JSON序列化DateTime类型数据(Ext4.2+ASP.NET MVC 4)

    最近项目中前台页面使用Extjs4.2 ,在后台ASP.NET MVC4返回的DateTime类型的数据错返回的DateTime类型的JsonResult的结果中的值是“\/Date(13784461 ...

  6. 【ASP.NET MVC系列】数据验证和注解

    [01]浅谈Google Chrome浏览器(理论篇) [02]浅谈Google Chrome浏览器(操作篇)(上) [03]浅谈Google Chrome浏览器(操作篇)(下) [04]浅谈ASP. ...

  7. Asp.Net Mvc 返回类型总结

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  8. ASP.NET MVC 描述类型(二)

    ASP.NET MVC 描述类型(二) 前言 上个篇幅中说到ControllerDescriptor类型的由来过程,对于ControllerDescriptor类型来言ActionDescriptor ...

  9. ASP.NET MVC 描述类型(一)

    ASP.NET MVC 描述类型(一) 前言 在前面的好多篇幅中都有提到过ControllerDescriptor类型,并且在ASP.NET MVC 过滤器(一)篇幅中简单的描述过,今天我们就来讲一下 ...

随机推荐

  1. 百度webuploader 上传演示例子

    前端代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="baiduWebU ...

  2. jeecg权限设置案例

    JEECG简单实例讲解权限控制 一.业务背景 某公司要实现一个日志系统,用来了解员工的工作量饱和情况. 二.需求 1.角色分为:员工.经理两种. 2.员工每天在日志系统中填报工作总结,然后经理进行点评 ...

  3. checkbox多选、全选js效果

    //全选checkbox function allCheck() { //全选input var all = $("input[name='all']"); //全部的input ...

  4. 获取url传的参数转变为对象的方法

    function GetRequest() { var url = location.search; //获取url中"?"符后的字串 var theRequest = new O ...

  5. 表单注册及自定义validate手机验证码验证实例

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...

  6. angularjs string format

    用惯了C#的string.format,在angularjs中还不太习惯字符串的拼接,还好可以自定义String.Format String.format = function() { ) retur ...

  7. webgl学习总结画线面及场景和物体动

    WebGL是在浏览器中实现三维效果的一套规范.是浏览器中的3D引擎,是利用js代码来实现加载3D模型,渲染.输出等功能,从而实现在浏览器和微信中浏览三维文件的效果. three.js是基于WebGL的 ...

  8. Xtrareport 交叉报表

    什么是交叉报表呢? 官方回答:交叉表报表是以交叉表形式呈现信息的报表. 交叉表 (或透视表) 类似于简单的普通数据绑定表格,但是改为在单个表格中呈现多维的分层级的信息,并含有每行和每列的自动排序.计数 ...

  9. iOS 上滑隐藏导航,下滑显示导航,仿斗鱼导航效果

    UItableView或 UIcollectionView 都是继承UIScrollView 滑动的时候,判断是上滑还是下滑 使用 UIScrollView 的代理方法 func scrollView ...

  10. maven配置好了之后再次开机找不到命令

    问题: 昨天还运行的好好的,今天突然又报错了, mvn不是内部或外部命令,也不是可运行程序或批处理文件 原因: 环境配置问题,windows7和windows10稍微有一点不一样,对照下面配置看哪里不 ...