ASP.NET WebService:不要抱怨日期格式

发布于 2024-09-28 15:32:01 字数 365 浏览 0 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千里故人稀 2024-10-05 15:32:01

XML 具有明确定义的日期和时间格式。客户没有任何借口以不同的格式发送日期。

  1. 如果没有解析日期,那么什么会传递给服务?
  2. 我不相信你。我从未见过 ASMX 服务对日期/时间格式有任何幽默感。请发布一些重现问题的代码。

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.

  1. If the date weren't parsed, then what would be passed to the service?
  2. I don't believe you. I have never seen an ASMX service have any sense of humor about date/time formats. Please post some code that reproduces the problem.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文