gpt4 book ai didi

asp.net-web-api - asp web api DateTime 模型绑定(bind)

转载 作者:行者123 更新时间:2023-12-04 17:22:48 25 4
gpt4 key购买 nike

我在 web api 中绑定(bind) DateTimes 时遇到了一些麻烦。这是情况。我有一个 Controller ,它返回一个具有 DateTime 属性的模型。我已经将我的 web api 设置为在 global.asax 中使用 IsoDateFormat 和 UTC 时间,如下所示:

        HttpConfiguration config = GlobalConfiguration.Configuration;
config.Formatters.JsonFormatter.SerializerSettings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat;
config.Formatters.JsonFormatter.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc;

日期时间格式以这种格式返回给客户端:
2013-02-04T11:24:48.91Z

在那方面一切都很好。但是,如果我以相同的格式将其发布回来,模型绑定(bind)器将无法识别该属性并将其保留为空。输入日期时间需要采用什么格式才能使默认的 DateTime 模型绑定(bind)起作用?

最佳答案

我已经按照您指定的方式配置了我的 Web API 服务,并且能够发布您指定格式的 DateTime。你有[FromBody]为您的 DateTime 参数?原始类型是 [FromUri]默认情况下。

public DateTime Post([FromBody]DateTime date)
{
return date;
}

要求:
POST http://localhost/api/values HTTP/1.1
content-type: application/json
Content-Length: 25

"2013-02-04T11:24:48.91Z"

回复:
HTTP/1.1 200 OK
Content-Length: 25
Content-Type: application/json; charset=utf-8

"2013-02-04T11:24:48.91Z"

关于asp.net-web-api - asp web api DateTime 模型绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14693255/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com