ASP.NET MVC的Json序列化的DateTime转换为UTC [英] ASP.NET MVC Json DateTime Serialization conversion to UTC

查看:240
本文介绍了ASP.NET MVC的Json序列化的DateTime转换为UTC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.NET MVC控制器使用JSON()方法给我的麻烦 - 在这个方法中抛出的每一个日期时间使用服务器的时间转换为UTC

Using Json() method on ASP.NET MVC Controller is giving me trouble - every DateTime thrown in this method is converted to UTC using server time.

现在,有一种简单的方法来告诉ASP.NET MVC JSON序列停止自动转换日期时间为UTC?因为它是在<著名href=\"http://stackoverflow.com/questions/10138562/json-in-mvc-converts-datetime-to-utc-automatically\">this reasigning每个变量与DateTime.SpecifyKind(日期,DateTimeKind.Utc)问题的伎俩,但显然我不能手动做到这一点每个日期时间变量。

Now, is there an easy way to tell ASP.NET MVC Json Serializer to stop auto-converting DateTime to UTC? As it is noted on this question reasigning each of your variable with DateTime.SpecifyKind(date, DateTimeKind.Utc) does the trick, but obviously I can't do this manually on every DateTime variable.

那么,是否可以设置在Web.config中的东西,有JSON序列对待每一个日期,如果是UTC?

So is it possible to set something in Web.config and have JSON serializer treat EVERY date as if it's UTC?

推荐答案

该死,看来最近我注定在计算器在这里回答自己的问题。叹了口气,这里是解决方案:

Dammit, it seems that lately I'm destined to answer my own questions here at StackOverflow. Sigh, here is the solution:


  1. 安装ServiceStack.Text使用的NuGet - 你会得到更快的JSON序列化免费(不客气)

  2. 一旦安装ServiceStack.Text,只是重写的Json方法在你的基地控制器(你有一个,对吧?):

  1. Install ServiceStack.Text using NuGet - you'll get faster JSON serialization for free (you're welcome)
  2. Once ServiceStack.Text is installed, just override Json method in your base Controller (you do have one, right?):

protected override JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior)
{
    return new ServiceStackJsonResult
    {
        Data = data,
        ContentType = contentType,
        ContentEncoding = contentEncoding
    };
}

public class ServiceStackJsonResult : JsonResult
{
    public override void ExecuteResult(ControllerContext context)
    {
        HttpResponseBase response = context.HttpContext.Response;
        response.ContentType = !String.IsNullOrEmpty(ContentType) ? ContentType : "application/json";

        if (ContentEncoding != null)
        {
            response.ContentEncoding = ContentEncoding;
        }

        if (Data != null)
        {
            response.Write(JsonSerializer.SerializeToString(Data));
        }
    }
}  


  • 看来,这串默认完成了正确的事 - 如果他们的DateTime.Kind是不确定它不惹你datetime对象。然而,我一些额外的配置调整在Global.asax中(和它的好,知道该怎么做,然后再开始使用库):

  • It seems that this serializer by default does "the right thing" - it doesn't mess with your DateTime objects if their DateTime.Kind is Unspecified. However, I did few additional config tweaks in Global.asax (and it's good to know how to do that before you start using library):

    protected void Application_Start()
    {
        JsConfig.DateHandler = JsonDateHandler.ISO8601;
        JsConfig.TreatEnumAsInteger = true;
    
        // rest of the method...
    }
    


  • 此链接帮助

    这篇关于ASP.NET MVC的Json序列化的DateTime转换为UTC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆