博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MVC解决Json DataGrid返回的日期格式是/Date(20130450000365)
阅读量:5103 次
发布时间:2019-06-13

本文共 5983 字,大约阅读时间需要 19 分钟。

实际上是Json格式化问题,我们应该在返回json的时候进行格式化,我们需要重写系统的JsonResult类

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Web.Mvc;using System.Text.RegularExpressions;using System.Web;using System.Web.Script.Serialization;namespace App.Common{    public class ToJsonResult : JsonResult    {        const string error = "该请求已被封锁,因为敏感信息透露给第三方网站,这是一个GET请求时使用的。为了可以GET请求,请设置JsonRequestBehavior AllowGet。";        ///         /// 格式化字符串        ///         public string FormateStr        {            get;            set;        }        public override void ExecuteResult(ControllerContext context)        {            if (context == null)            {                throw new ArgumentNullException("context");            }            if (JsonRequestBehavior == JsonRequestBehavior.DenyGet &&                String.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))            {                throw new InvalidOperationException(error);            }            HttpResponseBase response = context.HttpContext.Response;            if (!String.IsNullOrEmpty(ContentType))            {                response.ContentType = ContentType;            }            else            {                response.ContentType = "application/json";            }            if (ContentEncoding != null)            {                response.ContentEncoding = ContentEncoding;            }            if (Data != null)            {                JavaScriptSerializer serializer = new JavaScriptSerializer();                string jsonstring = serializer.Serialize(Data);                //string p = @"\\/Date\((\d+)\+\d+\)\\/";                string p = @"\\/Date\(\d+\)\\/";                MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertJsonDateToDateString);                Regex reg = new Regex(p);                jsonstring = reg.Replace(jsonstring, matchEvaluator);                response.Write(jsonstring);            }        }        ///         /// 将Json序列化的时间由/Date(1294499956278+0800)转为字符串        ///         private string ConvertJsonDateToDateString(Match m)        {            string result = string.Empty;            string p = @"\d";            var cArray = m.Value.ToCharArray();            StringBuilder sb = new StringBuilder();            Regex reg = new Regex(p);            for (int i = 0; i < cArray.Length; i++)            {                if (reg.IsMatch(cArray[i].ToString()))                {                    sb.Append(cArray[i]);                }            }            // reg.Replace(m.Value;            DateTime dt = new DateTime(1970, 1, 1);            dt = dt.AddMilliseconds(long.Parse(sb.ToString()));            dt = dt.ToLocalTime();            result = dt.ToString("yyyy-MM-dd HH:mm:ss");            return result;        }    }}

创建Controller的基类,BaseController让Controller去继承Controller

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Web;using System.Web.Mvc;using App.Common;using App.Models.Sys;namespace App.Admin{    public class BaseController : Controller    {        ///         /// 获取当前用户Id        ///         /// 
public string GetUserId() { if (Session["Account"] != null) { AccountModel info = (AccountModel)Session["Account"]; return info.Id; } else { return ""; } } /// /// 获取当前用户Name /// ///
public string GetUserTrueName() { if (Session["Account"] != null) { AccountModel info = (AccountModel)Session["Account"]; return info.TrueName; } else { return ""; } } /// /// 获取当前用户信息 /// ///
用户信息
public AccountModel GetAccount() { if (Session["Account"] != null) { return (AccountModel)Session["Account"]; } return null; } protected override JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior) { return new ToJsonResult { Data = data, ContentEncoding = contentEncoding, ContentType = contentType, JsonRequestBehavior = behavior, FormateStr = "yyyy-MM-dd HH:mm:ss" }; } /// /// 返回JsonResult.24 /// /// 数据 /// 行为 /// json中dateTime类型的格式 ///
Json
protected JsonResult MyJson(object data, JsonRequestBehavior behavior, string format) { return new ToJsonResult { Data = data, JsonRequestBehavior = behavior, FormateStr = format }; } /// /// 返回JsonResult42 /// /// 数据 /// 数据格式 ///
Json
protected JsonResult MyJson(object data, string format) { return new ToJsonResult { Data = data, FormateStr = format }; } /// /// 检查SQL语句合法性 /// /// ///
public bool ValidateSQL(string sql, ref string msg) { if (sql.ToLower().IndexOf("delete") > 0) { msg = "查询参数中含有非法语句DELETE"; return false; } if (sql.ToLower().IndexOf("update") > 0) { msg = "查询参数中含有非法语句UPDATE"; return false; } if (sql.ToLower().IndexOf("insert") > 0) { msg = "查询参数中含有非法语句INSERT"; return false; } return true; } }}

好了,我们运行,其实就是这么一回事!

转载于:https://www.cnblogs.com/ymnets/p/3439324.html

你可能感兴趣的文章
python常用函数
查看>>
python 描点画圆
查看>>
FastDFS使用
查看>>
服务器解析请求的基本原理
查看>>
pycharm 如何设置方法调用字体颜色
查看>>
VUE源码解析心得
查看>>
餐馆王第一天
查看>>
python 科学计数法转数值
查看>>
爱的十个秘密--8.沟通的力量
查看>>
mysql 自动加上编号
查看>>
Message no. C6015--No valuation variant found for valuation area xxxx
查看>>
Program Variant Scheduling job
查看>>
.net之路
查看>>
题目2-括号配对问题
查看>>
python 面向对象oop
查看>>
linux 安装 Django
查看>>
Heap:Sunscreen(POJ 3614)
查看>>
Storm-源码分析-Streaming Grouping (backtype.storm.daemon.executor)
查看>>
Hadoop TDG 2 – I/O
查看>>
C#中 As 和强制转换的总结
查看>>