29

我有这个网址

http://example.com/api/record/getall?startdate=1994-11-05T17:15:30Z

和这个 webapi 端点

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

我面临的问题是 startDate 将反序列化字符串作为本地时间接收,“ 11/5/1994 9:15:30 AM ”而不是停留在我想要的 UTC 时间“ 11/5/1994 5:15:晚上 30 点”。

我正在使用 VS2012 update2,最新的 Json.net nuget 包。但是,如果我在单独的控制台应用程序中使用 json.net 进行测试,则相同的字符串“ 1994-11-05T17:15:30Z ”能够正确反序列化为“ 11/5/1994 5:15:30 PM ”。

有人知道这里有什么问题吗?

4

3 回答 3

32

尽管您已经为您的问题找到了解决方案,但我想我会尝试解释为什么它没有按您的预期工作。

WebApi 使用内容类型协商来确定读取数据时使用的解析器。这意味着它将查看Content-Type请求的标头以做出决定。如果Content-Type标头设置为,application/json那么它将使用 Json.Net 解析内容并将其提供给您的方法。

HTTP GET 请求(例如您在此处发出的请求)没有设置内容类型。在这种情况下,“内容”实际上只是来自 URL 的查询字符串。WebApi 不希望在这里找到 JSON 数据,因此它不会尝试使用 JSON 解析器来理解它。即使是这样,您传递给 GetAll 方法的字符串也不是有效的 JSON。(它需要被引用才能有效。)

现在,如果您要更改方法以接受 POST 请求,并将内容类型标头设置为application/json并将日期作为 JSON 字符串传递到正文中,那么 WebApi 将使用 Json.Net 对其进行解析,并且它将起作用如你所料。

例如,假设您的方法如下所示:

[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):

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。

于 2013-05-31T01:37:26.237 回答
5

如果要修改 Asp WebApi 解析 GET 请求的 uri 参数的方式,可以IModelBinder这样编写自定义:

public class UtcDateTimeModelBinder : IModelBinder
{
    public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
    {
        var stringValue = bindingContext.ValueProvider.GetValue(bindingContext.ModelName)?.RawValue as string;
        DateTime parsedDate;
        if (!DateTime.TryParse(stringValue, null, DateTimeStyles.AdjustToUniversal, out parsedDate))
        {
            return false;
        }

        bindingContext.Model = parsedDate;
        return true;
    }
}

然后覆盖默认活页夹:

GlobalConfiguration.Configure(configuration => configuration.BindParameter(typeof(DateTime), new UtcDateTimeModelBinder()););

ASP.NET Web API 中的参数绑定

于 2016-08-19T04:39:43.910 回答
0

只需使用DateTimeOffset而不是DateTime作为 API 的输入类型:

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

将查询字符串中的日期作为 UTC 传递,例如

startDate=2021-10-01T12:00:00.000Z

这将创建一个 C#DateTimeOffset并且其UtcDateTime属性将具有 UTC DateTime

( 的LocalDateTime属性DateTimeOffset也将是 UTC DateTime,而 的Offset属性DateTimeOffset将是 的 a TimeSpan00:00:00这与 a 不同DateTime,后者的Kind属性设置Utc为 UTC 日期时间)

UsingDateTimeOffset还具有允许将日期作为本地时间传递的优点,例如可以将以下日期传递给 API:

startDate=2021-10-01T18:00:00.000+05:00

请注意,在查询字符串中传递 this 时,+必须将 URL 编码为%2B

startDate=2021-10-01T18:00:00.000%2B05:00

这将转换为DateTimeOffset代表 UTC 前 5 小时时区中的本地时间:

startDate.DateTime = {1/10/21 6:00:00 pm}
startDate.UtcDateTime = {1/10/21 1:00:00 pm}
startDate.LocalDateTime = {1/10/21 1:00:00 pm}
startDate.Offset = {05:00:00}
于 2021-10-11T23:52:28.087 回答