ASP.NET WebService:不要抱怨日期格式
我的 Web 服务方法中的字段之一接受 DateTime 值。一旦服务用户以错误的格式提供了值:“31-10-2010”(或者可能是“31-10-10”,说实话:我不确定,现在正在实施日志记录)。它是“dd-mm-yyyy”而不是“标准 XML 日期时间是“yyyy-mm-ddTHH:MM:SS”。
问题是 Web 服务并没有将此类日期视为“错误”(或无效),而是成功解析了此字符串作为有效日期,它被解析为“2011 年 1 月 31 日上午 12:10”。
问题 1. 为什么要解析日期?
问题 2. 如何防止这种情况发生?
PS 我已经实现了检查日期的业务逻辑。处于某个有意义的范围内(从现在起不超过 10 年),但这并不总是有帮助......
One of the field in my web service method accepts DateTime value. Once service user provided value in wrong format: "31-10-2010" (or probably it was "31-10-10", to be honest: I not sure, are implementing logging right now). It is "dd-mm-yyyy" instead of "standard XML datetime been "yyyy-mm-ddTHH:MM:SS".
The problem that instead of rejecting such date as "wrong" (or invalid) web service successfully parsed this string as valid date, it was parsed as "31 of January, 2011 12:10 AM".
Question 1. Why the date is parsed at all?
Question 2. How to prevent that?
P.S. I've implemented business logic that check date to be in some meaningful range (no more then 10 years from now), but this don't help always...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
XML 具有明确定义的日期和时间格式。客户没有任何借口以不同的格式发送日期。
XML has a well-defined format for dates and times. There is no excuse whatsoever for a client to send a date in a different format.