解决MVC 时间序列化的方法
2015-06-12 16:51
351 查看
1.全局处理
处理代码
在Application_Start调用SetSerializationJsonFormat(GlobalConfiguration.Configuration)全局设置
2.重写ActionResult
处理代码
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; } } }
相关文章推荐
- Delphi中Variant/OleVariant转换为接口
- BEX错误记录
- Qt Runtime Error ! R6025 - pure virtual function call
- 逆波兰表示法
- PHP 数组操作方法
- 一些常用的IOS开发网站
- 调整框架的尺寸
- detail屏显示行数条数
- GRE issue写作范文:竞争利弊问题
- 你们猜对了:12306确实是让淘宝做的
- 1037. Magic Coupon (25)
- 全球移动应用用户参与度分析
- 1037. Magic Coupon (25)
- 大数据
- www.java2s.com
- java 利用反射机制,获取实体所有属性和方法,并对属性赋值
- NSNotificationCenter 的使用详解
- UI基础—UITableView
- cocos node sceen
- Python模块学习 ---- datetime