问题背景:

在使用asp.net mvc 结合jquery esayui做一个系统,但是在使用使用this.json方法直接返回一个json对象,在列表中显示时发现datetime类型的数据在转为字符串是它默认转为Date(84923838332223)的格式,在经过查资料发现使用前端来解决这个问题的方法不少,但是我又发现在使用jquery easyui时,加载列表数据又不能对数据进行拦截,进行数据格式转换之后再加载,后来发现可以通过自定义JsonResult实现,认为这种方法比较可行,就开始研究

我们先来看看jsonResult的源码

 1     public class JsonResult : ActionResult
2 {
3 public JsonResult()
4 {
5 this.JsonRequestBehavior = System.Web.Mvc.JsonRequestBehavior.DenyGet;
6 }
7
8 public override void ExecuteResult(ControllerContext context)
9 {
10 if (context == null)
11 {
12 throw new ArgumentNullException("context");
13 }
14 if ((this.JsonRequestBehavior == System.Web.Mvc.JsonRequestBehavior.DenyGet) && string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
15 {
16 throw new InvalidOperationException(MvcResources.JsonRequest_GetNotAllowed);
17 }
18 HttpResponseBase response = context.HttpContext.Response;
19 if (!string.IsNullOrEmpty(this.ContentType))
20 {
21 response.ContentType = this.ContentType;
22 }
23 else
24 {
25 response.ContentType = "application/json";
26 }
27 if (this.ContentEncoding != null)
28 {
29 response.ContentEncoding = this.ContentEncoding;
30 }
31 if (this.Data != null)
32 {
33 JavaScriptSerializer serializer = new JavaScriptSerializer();
34 response.Write(serializer.Serialize(this.Data));
35 }
36 }
37
38 public Encoding ContentEncoding { get; set; }
39
40 public string ContentType { get; set; }
41
42 public object Data { get; set; }
43
44 public System.Web.Mvc.JsonRequestBehavior JsonRequestBehavior { get; set; }
45 }
46 }

当我看到上面代码中的红色部分,我感到有些熟悉,心里比较高兴,以前使用过ashx来传json的都应该用过此方法吧

原来它也是使用这个方法进行序列化的。我们就可以在这个地方先获取到json序列化之后的字符串!然后做写“小动作”,就ok了

下面我就定义了一个自己的JsonResult了

 1     /// <summary>
2 /// 自定义Json视图
3 /// </summary>
4 public class CustomJsonResult:JsonResult
5 {
6 /// <summary>
7 /// 格式化字符串
8 /// </summary>
9 public string FormateStr
10 {
11 get;
12 set;
13 }
14
15 /// <summary>
16 /// 重写执行视图
17 /// </summary>
18 /// <param name="context">上下文</param>
19 public override void ExecuteResult(ControllerContext context)
20 {
21 if (context == null)
22 {
23 throw new ArgumentNullException("context");
24 }
25
26 HttpResponseBase response = context.HttpContext.Response;
27
28 if (string.IsNullOrEmpty(this.ContentType))
29 {
30 response.ContentType = this.ContentType;
31 }
32 else
33 {
34 response.ContentType = "application/json";
35 }
36
37 if (this.ContentEncoding != null)
38 {
39 response.ContentEncoding = this.ContentEncoding;
40 }
41
42 if (this.Data != null)
43 {
44 JavaScriptSerializer jss = new JavaScriptSerializer();
45 string jsonString = jss.Serialize(Data);
46 string p = @"\\/Date\((\d+)\)\\/";
47 MatchEvaluator matchEvaluator = new MatchEvaluator(this.ConvertJsonDateToDateString);
48 Regex reg = new Regex(p);
49 jsonString = reg.Replace(jsonString, matchEvaluator);
50
51 response.Write(jsonString);
52 }
53 }
54
55 /// <summary>
56 /// 将Json序列化的时间由/Date(1294499956278)转为字符串 .
57 /// </summary>
58 /// <param name="m">正则匹配</param>
59 /// <returns>格式化后的字符串</returns>
60 private string ConvertJsonDateToDateString(Match m)
61 {
62 string result = string.Empty;
63 DateTime dt = new DateTime(1970, 1, 1);
64 dt = dt.AddMilliseconds(long.Parse(m.Groups[1].Value));
65 dt = dt.ToLocalTime();
66 result = dt.ToString(FormateStr);
67 return result;
68 }
69 }

在这里做的“小动作”就是红色部分,得到字符串以后,通过正则表达式的方式获得Date(12347838383333)的字符串,然后把它转换为DateTime类型,最后在转为我们想要的格式即可,这个格式可以使用FormateStr属性设置。

剩下的就是使用我们自己定义的JsonResult来替换asp.net mvc默认的JsonResult的问题了,接着从源码中找答案,下面是Controller类的部分代码

 1         protected internal JsonResult Json(object data)
2 {
3 return this.Json(data, null, null, JsonRequestBehavior.DenyGet);
4 }
5
6 protected internal JsonResult Json(object data, string contentType)
7 {
8 return this.Json(data, contentType, null, JsonRequestBehavior.DenyGet);
9 }
10
11 protected internal JsonResult Json(object data, JsonRequestBehavior behavior)
12 {
13 return this.Json(data, null, null, behavior);
14 }
15
16 protected internal virtual JsonResult Json(object data, string contentType, Encoding contentEncoding)
17 {
18 return this.Json(data, contentType, contentEncoding, JsonRequestBehavior.DenyGet);
19 }
20
21 protected internal JsonResult Json(object data, string contentType, JsonRequestBehavior behavior)
22 {
23 return this.Json(data, contentType, null, behavior);
24 }
25
26 protected internal virtual JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior)
27 {
28 return new JsonResult { Data = data, ContentType = contentType, ContentEncoding = contentEncoding, JsonRequestBehavior = behavior };
29 }

以上是Controller类来实例化JsonResult的所有代码。我们只需写一个BaseController类,重写最后一个方法即可,然后我们自己的Controller在继承BaseController即可

下面是BaseController类的部分代码,我们为方便自己个性化的需要又定义了两个MyJosn的方法

 1   /// <summary>
2 /// 返回JsonResult
3 /// </summary>
4 /// <param name="data">数据</param>
5 /// <param name="contentType">内容类型</param>
6 /// <param name="contentEncoding">内容编码</param>
7 /// <param name="behavior">行为</param>
8 /// <returns>JsonReuslt</returns>
9 protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding, JsonRequestBehavior behavior)
10 {
11 return new CustomJsonResult
12 {
13 Data = data,
14 ContentType = contentType,
15 ContentEncoding =contentEncoding,
16 JsonRequestBehavior = behavior,
17 FormateStr = "yyyy-MM-dd HH:mm:ss"
18 };
19 }
20
21 /// <summary>
22 /// 返回JsonResult.24 /// </summary>
25 /// <param name="data">数据</param>
26 /// <param name="behavior">行为</param>
27 /// <param name="format">json中dateTime类型的格式</param>
28 /// <returns>Json</returns>
29 protected JsonResult MyJson(object data, JsonRequestBehavior behavior,string format)
30 {
31 return new CustomJsonResult
32 {
33 Data = data,
34 JsonRequestBehavior = behavior,
35 FormateStr = format
36 };
37 }
38
39 /// <summary>
40 /// 返回JsonResult42 /// </summary>
43 /// <param name="data">数据</param>
44 /// <param name="format">数据格式</param>
45 /// <returns>Json</returns>
46 protected JsonResult MyJson(object data, string format)
47 {
48 return new CustomJsonResult
49 {
50 Data = data,
51 FormateStr = format
52 };
53 }

最后我们在自己的Controller中调用即可

 1 public class ProjectMileStoneController : BaseController
2 {
3 /// <summary>
4 /// 首页视图
5 /// </summary>
6 /// <returns>视图</returns>
7 public ActionResult Index()
8 {
9 return this.View();
10 }
11
12 #region 项目里程碑查询
13
14 /// <summary>
15 /// 根据项目编号获取项目里程碑
16 /// </summary>
17 /// <param name="projectId">项目编号</param>
18 /// <returns>项目里程碑</returns>
19 public JsonResult GetProjectMileStoneByProjectId(int projectId)
20 {
21 IList<ProjectMileStone> projectMileStones = FacadeContainer.Get<IProjectMileStoneService>().GetProjectMileStonesByProjectId(projectId);
22 return this.MyJson(projectMileStones, "yyyy.MM.dd");
23 }
24
25 #endregion
26 }

----转载出处http://www.cnblogs.com/JerryWang1991/

解决Asp.net Mvc返回JsonResult中DateTime类型数据格式的问题的更多相关文章

  1. 用JS解决Asp.net Mvc返回JsonResult中DateTime类型数据格式的问题

    当用ajax异步时,返回JsonResult格式的时候,发现当字段是dateTime类型时,返回的json格式既然是“/Date(1435542121135)/” 这样子的,当然这不是我们想要的格式. ...

  2. 解决ASP.NET MVC返回的JsonResult 中 日期类型数据格式问题,和返回的属性名称转为“驼峰命名法”和循环引用问题

    DateTime类型数据格式问题 问题 在使用ASP.NET MVC 在写项目的时候发现,返回给前端的JSON数据,日期类型是 Date(121454578784541) 的格式,需要前端来转换一下才 ...

  3. ASP.NET MVC 返回JsonResult序列化内容超出最大限制报错的解决办法

    在使用MVC的时候我们经常会在Controller的Action方法中返回JsonResult对象,但是有时候你如果序列化的对象太大会导致JsonResult从Controller的Action返回后 ...

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

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

  5. ASP.NET MVC开发学习过程中遇到的细节问题以及注意事项

    1.datagrid中JS函数传值问题: columns: { field: 'TypeName', title: '分类名称', width: 120, sortable: true, format ...

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

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

  7. C# WebAPI中DateTime类型字段在使用微软自带的方法转json格式后默认含T的解决办法

    原文:C# WebAPI中DateTime类型字段在使用微软自带的方法转json格式后默认含T的解决办法 本人新手,在.Net中写WebAPI的时候,当接口返回的json数据含有日期时间类型的字段时, ...

  8. 在ASP.NET MVC应用程序中实现Server.Transfer()类似的功能

    在ASP.NET MVC应用程序中,如果使用Server.Transfer()方法希望将请求转发到其它路径或者Http处理程序进行处理,都会引发“为xxx执行子请求时出错”的HttpException ...

  9. ASP.NET MVC 3: Razor中的@:和语法

    原文 ASP.NET MVC 3: Razor中的@:和语法 [原文发表地址] ASP.NET MVC 3: Razor’s @: and <text> syntax[原文发表时间] De ...

随机推荐

  1. sftp的安装和使用

    http://blog.srmklive.com/2013/04/24/how-to-setup-sftp-server-ftp-over-ssh-in-ubuntu/ In my previous ...

  2. Tomcat常见内存溢出的解决办法

    PermGen space错误解决方法 在看下文之前,首先要确认意见事情,就是你是如何启动tomcat的,我们在平时的开发环境当中,都是通过startup.bat方式启动tomcat的,那么你按照下面 ...

  3. CactiEZ命令行添加主机监控参考

    1.添加主机 php -q add_device.php --description= --community="public" 查询主机模板: php -q add_device ...

  4. 《Java编程那点事儿》读书笔记(四)——String和StringBuffer

    String 1.toString:显示对象内容时系统自动调用的方法. public class TOSTRING { public String toString(){ return "t ...

  5. js 去空格函数与正则

    如果项目没有用到jQuery等框架的话,js本身又没有这样的函数,我们不得不自己写这样的函数,下面是函数的具体实现: //供使用者调用 function trim(s){ return trimRig ...

  6. 安装hma master出错 Error: Package: perl-Mail-Sender-0.8.13-2.el5.1.noarch

    You are using the EPEL 5 version of the repo instead of 6, go into your /etc/yum.repos.d/epel.repo f ...

  7. BZOJ 1878 HH的项链(树状数组)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1878 题意:给出一个数列,每次询问区间[L,R]中有多少个不同的数字? 思路: (1)记 ...

  8. base64加密和解密

    http://snailwarrior.blog.51cto.com/680306/142472/ 2.从标准输入读取文件内容,base64编码并打印到标准输出 [root@localhost tes ...

  9. 第五讲:深入hibernate的三种状态

    学过hibernate的人都可能都知道hibernate有三种状态,transient(瞬时状态),persistent(持久化状态)以及detached(离线状态),大家伙也许也知道这三者之间的区别 ...

  10. ListView 连接数据库的增删改查

    private string link = "server=.;database=list;user=sa;pwd=123"; public void chaxun() //创建一 ...