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. this与$scope

    最近在Angular项目中遇到关于controller内使用$scope&this 暴露数据的问题,下面来分析一下: "controller as" 是Angular在1. ...

  2. Lintcode: Find Peak Element

    There is an integer array which has the following features: * The numbers in adjacent positions are ...

  3. discuz模板引擎

    discuz是采用“编译型的模板”,就是指采用普通网页文件保存,在文件中插入需要动态显示数据的仿php的代码,最后进行编译成真正的php的文件保存为模板缓存文件,这个转换的过程就称为编译.在用户浏览页 ...

  4. c#中WMI 中的日期和时间转为本地时间

    取得的值:CreationDate:20170122084915 .713600+480 转:   var objQuery = new ObjectQuery("select * from ...

  5. 002-ubuntu安装

    一.安装了ubuntu desktop版本后: 1.进行桥接联网. 2.运行更新:#sudo apt-get update. 3.安装net-tools网络工具包:#sudo apt install ...

  6. Linux基础命令---bc

    bc bc是一种算数语言,其语法和c语言类似,可以交互执行.通过命令行选项可以获得一个标准的数学库.如果请求,在处理任何文件之前定义数学库.BC从处理所有文件的代码开始.命令行中列出的文件按所列顺序排 ...

  7. C/C++之内存对齐

    数据对齐,是指数据所在的内存地址必须是该数据长度的整数倍.DWORD数据的内存起始地址能被4除尽,WORD数据的内存起始地址能被2除尽.X86 CPU能直接访问对齐的数据,当它试图访问一个未对齐的数据 ...

  8. HTTP从入门到入土(5)——HTTP报文格式

    HTTP报文格式 HTTP报文分为请求报文和响应报文,只有发送了请求报文,才会有响应报文. 常见的报文格式如下所示:

  9. Python3.x:import urllib2报错解决方案

    Python:import urllib2报错解决方案 python2和3有些不一样: python2:输出为print 'hello world' python3:输出为print('hello w ...

  10. 函数对象与仿函数(function object and functor)

    part 1. 仿函数在STL组件中的关系 如下图: # 仿函数配合算法完成不同的策略变化. # 适配器套接仿函数. part 2. 仿函数介绍 传递给算法的“函数型实参”不一定得是函数,可以是行为类 ...