且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

传递DateTimeOffset作为WebAPI查询字符串

更新时间:2023-02-26 11:21:23

该问题已由400响应消息准确描述,尽管可能更清楚了。根据属性定义,该路线仅需要一个参数 id ,但是Delete方法需要一个名为 date 的参数。

The problem is being described exactly by the 400 response message, although it could have been more clear. The route, as defined by the attribute, only expects a parameter id, but the Delete method expects another parameter called date.

如果要使用查询字符串提供此值,则需要使用 DateTimeOffset?使该参数为可空值,这还将其转换为可选参数。如果日期是必填字段,请考虑将其添加到路线中,例如:

If you want to provide this value using the query string, you'll need to make that parameter nullable, by using "DateTimeOffset?", which would also transform it into an optional parameter. If the date is a required field, consider adding it to the route, like:

[Route("api/values/{id}/{date}")]

好,忽略我在上面键入的内容,这只是一个格式问题。 Web API无法弄清楚解析给定值所需的区域性,但是如果您尝试在查询字符串中使用JSON格式(例如2014-05-06T22:24:55Z)传递DateTimeOffset,那应该可以。

OK, ignore what I typed above, it's just a formatting problem. Web API has trouble figuring out the culture needed to parse the given value, but if you try to pass on DateTimeOffset using a JSON format in the query string, like 2014-05-06T22:24:55Z, that should work.