我们有许多带有 Typescript 模型的 Vue 页面,它们提交到相同的 Controller 操作。该 Controller 采用单个复杂参数,并且该类具有 DateTime 属性。
随着时间的推移,Vue 页面已经被修改,现在看起来彼此完全不同。但是,在所有情况下,DateTime 属性均取自 inputDateOfBirth 字段并与 Moment 进行映射。代码看起来有点像这样:
var dob = Helpers.ToMoment(this.inputDateOfBirth);
if (dob.isValid()) {
this.dateOfBirth = dob.format("YYYY-MM-DD HH:mm:ss");
}
dateOfBirth
和 inputDateOfBirth
都在 Typescript 模型中列为 string
类型。
在大多数情况下,最终由 Controller 操作接收的复杂参数的 DateTime 属性为 Utc
的 DateTime.Kind
。然而,其中之一的 DateTime.Kind
为 Unspecified
。这会导致进一步的问题。
什么决定了 ASP.NET MVC 中 C# Controller 操作解释的数据的 DateTime.Kind 值,我可以采取哪些措施来确保它以 Utc 形式一致接收?
最佳答案
日期格式可能会导致这种差异。
DateTime 的默认模型绑定(bind)器需要 ISO 8601 时区指示符
,这意味着如果您的日期字符串类似于 2009-06-15T13:45:30.0000000Z
,则 DateTime.Kind
将UTC
表示为“Z”。
或者,如果像这样 2009-06-15T13:45:30.0000000-07:00
,其中时区偏移量为“-07:00”,则 DateTime.Kind
将是 本地
。
如果没有提供此属性,DateTime.Kind
将为Unspecified
。
关于c# - 当 ASP.Net 收到具有 DateTime 属性的已发布模型时,什么决定了 DateTime.Kind?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61462849/