1.全局处理

处理代码

  publict static void SetSerializationJsonFormat(HttpConfiguration config)
{
// Web API configuration and services
var json = config.Formatters.JsonFormatter;
////(时间格式只支持2种)json.SerializerSettings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat;
////时间格式("自定义格式")
json.SerializerSettings.Converters.Add(new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" });
////移除json序列化器
////config.Formatters.Remove(config.Formatters.JsonFormatter);
}

在Application_Start调用SetSerializationJsonFormat(GlobalConfiguration.Configuration)全局设置

2.重写ActionResult

     public class JsonResult : ActionResult
{
public JsonResult()
{
this.ContentEncoding = Encoding.UTF8;
this.ContentType = "application/json";
} public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
} HttpResponseBase response = context.HttpContext.Response;
if (!string.IsNullOrEmpty(this.ContentType))
{
response.ContentType = this.ContentType;
}
if (this.ContentEncoding != null)
{
response.ContentEncoding = this.ContentEncoding;
}
if (this.Data != null)
{
response.Write(NewtonsoftSerialize(this.Data));
response.End();
}
} public Encoding ContentEncoding { get; set; } public string ContentType { get; set; } public object Data { get; set; } private static string NewtonsoftSerialize(object value)
{
try
{
IsoDateTimeConverter timeFormat = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
return JsonConvert.SerializeObject(value, timeFormat);
}
catch
{
return string.Empty;
}
}
}

解决MVC 时间序列化的方法的更多相关文章

  1. 解决MVC Json序列化的循环引用问题/EF Json序列化循引用问题---Newtonsoft.Json

    1..Net开源Json序列化工具Newtonsoft.Json中提供了解决序列化的循环引用问题: 方式1:指定Json序列化配置为 ReferenceLoopHandling.Ignore 方式2: ...

  2. ASP.NET MVC 5 - 验证编辑方法(Edit method)和编辑视图(Edit view)

    在本节中,您将验证电影控制器生成的编辑方法(Edit action methods)和视图.但是首先将修改点代码,使得发布日期属性(ReleaseDate)看上去更好.打开Models \ Movie ...

  3. [转]ASP.NET MVC 5 - 验证编辑方法(Edit method)和编辑视图(Edit view)

    在本节中,您将验证电影控制器生成的编辑方法(Edit action methods)和视图.但是首先将修改点代码,使得发布日期属性(ReleaseDate)看上去更好.打开Models \ Movie ...

  4. 解决MVC中JSON字符长度超出限制的异常

    解决MVC中JSON字符长度超出限制的异常 解决方法如下: <configuration> <system.web.extensions> <scripting> ...

  5. MVC验证09-使用MVC的Ajax.BeginForm方法实现异步验证

    原文:MVC验证09-使用MVC的Ajax.BeginForm方法实现异步验证 MVC中,关于往后台提交的方法有: 1.Html.BeginForm():同步 2.Ajax.BeginForm():异 ...

  6. MVC+EF 序列化类型为“System.Data.Entity.DynamicProxies.__的对象时检测到循环引用

    用MVC+EF做简单查询时,返回json格式数据出现问题 原代码: public ActionResult JSon({ NorthwindEntities db = new NorthwindEnt ...

  7. 两种序列化的方法 Serializable Parcelable

    原文:https://blog.csdn.net/hacker_crazy/article/details/80840868 一.Serializable 1.Serializable 是java的序 ...

  8. SpringBoot的json序列化及时间序列化处理

    使用场景:前台的多种时间格式存入后台,后台返回同时的时间格式到前台. 1 在config目录下配置jscksonConfig.java package com.test.domi.config; im ...

  9. Ubuntu下Sublime Text 3解决无法输入中文的方法

    Ubuntu下Sublime Text 3解决无法输入中文的方法_百度经验http://jingyan.baidu.com/article/f3ad7d0ff8731609c3345b3b.html ...

随机推荐

  1. windows7触屏操作API

    每当用户触摸触敏式 Windows 7 设备时,Windows 7 多点触控平台都会向您的应用程序发送手势消息 WM_GESTURE.这是现成的免费行为,如果您希望停止接收此类消息,则需要选择退出. ...

  2. numpy的ravel()和flatten()函数

    相同点: 两者所要实现的功能是一致的(将多维数组降位一维).这点从两个单词的意也可以看出来,ravel(散开,解开),flatten(变平). In [14]: x=np.array([[1,2],[ ...

  3. css定位浮动总结

    定位:定位在中间,放大缩小时也不会跑偏. position:absolute; top: 50%; left: 50%; margin: -270px 0 0 -455px; 解释:定位后,设百分比的 ...

  4. C语言常用函数大全

    一.数学函数 调用数学函数时,要求在源文件中包下以下命令行: #include <math.h> 函数原型说明 功能 返回值 说明 int abs( int x) 求整数x的绝对值 计算结 ...

  5. linux常用命令:netstat 命令

    netstat命令用于显示与IP.TCP.UDP和ICMP协议相关的统计数据,一般用于检验本机各端口的网络连接情况.netstat是在内核中访问网络及相关信息的程序,它能提供TCP连接,TCP和UDP ...

  6. Linux基础命令---diff

    diff 逐行比较两个文本文件,把文件的差异显示到标准输出.如果要指定要比较目录,那么diff命令会比较目录中相同文件名的文件,不会比较子目录. 此命令的适用范围:RedHat.RHEL.Ubuntu ...

  7. LLVM/Clang编译相关研究

    https://blog.csdn.net/guojin08/article/details/54310858 https://juejin.im/entry/5c64da44518825620b45 ...

  8. SpringBoot——定时任务+WebSocket(问题)

    开发环境:win7 + idea2018 + jdk 1.8 + springboot 2.x 记一次出现问题,我在项目中先集成了websocket环境,并且测试通过,之后想要模拟实时推送的效果,虽然 ...

  9. MySQL笔记(五)MySQL 角色与SQL CHECK约束

    MySQL ROLE MySQL 8.0 Reference Manual  /  Security  /  MySQL User Account Management  /  Using Roles ...

  10. Centos6版本使用yum报错 Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfi Setting up Install Process No package gcc available. Error: Nothing to do

    在使用Centos6版本yum时报错 Loaded plugins: fastestmirror, refresh-packagekit, securityLoading mirror speeds ...