Asp.net的WebAPI反序列UTC时间字符串为本地时间 [英] Asp.net WebApi deserializes UTC time string to local time

查看:183
本文介绍了Asp.net的WebAPI反序列UTC时间字符串为本地时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个网址

<一个href=\"http://mysite.com/api/record/getall?startdate=1994-11-05T17:15:30Z\">http://mysite.com/api/record/getall?startdate=1994-11-05T17:15:30Z

和这个端点的WebAPI

and this webapi endpoint

[ActionName("GetAll")]
public object GetAll(DateTime startDate)
    {
     ...
    }

我面临的问题是,在的startDate UTC时间这正是我想要收到反序列化的字符串作为本地时间, 1994年11月5日上午9时15分三十秒,而不是留 1994年11月5日下午5点十五分30秒

我使用VS2012 UPDATE2,介绍最新Json.net的NuGet包。不过,如果我使用json.net在一个单独的控制台应用程序进行测试,相同的字符串 1994-11-05T17:15:30Z 是能够正确地反序列化为 11/5 / 1994年下午五时十五分30秒

I'm using VS2012 update2, lastest Json.net nuget package. However, if I use json.net in a separate console app to test, the same string "1994-11-05T17:15:30Z" is able to deserialize correctly into "11/5/1994 5:15:30 PM".

任何人都知道这里有什么问题?

Anyone know what is wrong here?

谢谢,
雷。

推荐答案

虽然你已经找到了你的问题的解决方案,我想我会采取射击在解释为什么它,你没有按预期工作。

Although you have already found a solution for your question, I thought I would take a shot at explaining why it did not work as you expected.

的WebAPI使用内容类型的协商,以确定哪些解析器读取数据时使用。这意味着它会看来做出决定请求的内容类型头。如果内容类型头设置为应用程序/ JSON 它将使用Json.Net解析内容和其提供给您的方法。

WebApi uses content type negotiation to determine what parser to use when reading data. That means it will look at the Content-Type header of the request to make the determination. If the Content-Type header is set to application/json then it will use Json.Net to parse to content and feed it to your method.

这是HTTP GET请求,比如你在这里做的人,没有一个内容类型集。 内容,在这种情况下,实际上只是从URL查询字符串。的WebAPI没有想到会在这里找到JSON数据,因此它不会尝试使用JSON解析器来理解它。即使做了,你传递给你的GETALL方法的字符串甚至不是有效的JSON。 (这将需要报价才有效。)

An HTTP GET request, such as the one you are making here, does not have a content type set. The "content" in this case is really just the query string from the URL. WebApi does not expect to find JSON data here, so it is not going to try to use a JSON parser to make sense of it. Even if it did, the string you are passing to your GetAll method isn't even valid JSON. (It would need to be quoted to be valid.)

现在,如果你要改变你的方法接受一个POST请求,并设置内容类型头应用程序/ JSON 和通过的日期为JSON字符串在体内,然后将的WebAPI用Json.Net解析它,它会像你期望。

Now, if you were to change your method to accept a POST request, and you set the content type header to application/json and passed the date as a JSON string in the body, then WebApi will use Json.Net to parse it, and it will work like you expect.

例如,说你的方法是这样的:

For example, say your method looked like this:

[HttpPost]
public object GetAll([FromBody]DateTime startDate)
{
    try
    {
        return new
        {
            StartDate = startDate.ToString("yyyy-MM-dd HH:mm:ss"),
            StartDateKind = startDate.Kind.ToString(),
        };
    }
    catch (Exception ex)
    {
        return ex.Message;
    }
}

和你做这样的请求(注意POST):

And you made a request like this (note the POST):

POST http://localhost:57524/api/values/GetAll HTTP/1.1
Content-Type: application/json
Content-Length: 22
Host: localhost:57524

"1994-11-05T17:15:30Z"

回应是这样的:

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Fri, 31 May 2013 01:25:48 GMT
Content-Length: 57

{"StartDate":"1994-11-05 17:15:30","StartDateKind":"Utc"}

正如你所看到的,它不正确识别日期为UTC在这种情况下。

As you can see, it does correctly recognize the date to be UTC in this scenario.

这篇关于Asp.net的WebAPI反序列UTC时间字符串为本地时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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