UTC传递到的DateTime API网络HTTPGET方法导致本地时间 [英] Passing UTC DateTime to Web API HttpGet Method results in local time
问题描述
我想传递一个UTC日期作为查询字符串参数到Web API方法。该URL看起来像
I'm trying to pass a UTC date as a query string parameter to a Web API method. The URL looks like
/api/order?endDate=2014-04-01T00:00:00Z&zoneId=4
该方法的签名看起来像
The signature of the method looks like
[HttpGet]
public object Index(int zoneId, DateTime? endDate = null)
日期快到了以 31/03/2014下午8时00分00秒
,但我想它来作为 01 / 04/2014年12:00:00 AM
我的 JsonFormatter.SerializerSettings
看起来像这样
new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
DateTimeZoneHandling = DateTimeZoneHandling.Utc,
DateFormatHandling = DateFormatHandling.IsoDateFormat
};
编辑#1:
我注意到,当我POST 2014-04-01T00:00:00Z
将序列化到UTC的DateTime那种在C#。但是我发现这样做的一个变通 endDate.Value.ToUniversalTime()
来转换它,虽然我觉得很奇怪它是如何工作的POST而不是GET。
EDIT #1:
I've noticed when I POST 2014-04-01T00:00:00Z
it will serialize to the UTC DateTime kind in C#. However I've found a work around of doing endDate.Value.ToUniversalTime()
to convert it although I find it odd how it works for a POST but not a GET.
推荐答案
我最终只使用 ToUniversalTime()
方法参数进来了。
I ended up just using the ToUniversalTime()
method as parameters come in.
这篇关于UTC传递到的DateTime API网络HTTPGET方法导致本地时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!