c# - 当 ASP.Net 收到具有 DateTime 属性的已发布模型时,什么决定了 DateTime.Kind?

标签 c# asp.net asp.net-mvc datetime momentjs

我们有许多带有 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");
}

dateOfBirthinputDateOfBirth 都在 Typescript 模型中列为 string 类型。

在大多数情况下,最终由 Controller 操作接收的复杂参数的 DateTime 属性为 UtcDateTime.Kind。然而,其中之一的 DateTime.KindUnspecified。这会导致进一步的问题。

什么决定了 ASP.NET MVC 中 C# Controller 操作解释的数据的 DateTime.Kind 值,我可以采取哪些措施来确保它以 Utc 形式一致接收?

最佳答案

日期格式可能会导致这种差异。
DateTime 的默认模型绑定(bind)器需要 ISO 8601 时区指示符,这意味着如果您的日期字符串类似于 2009-06-15T13:45:30.0000000Z,则 DateTime.KindUTC表示为“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/

相关文章:

c# - 在属性和方法之间共享角色属性的初始化

c# - 我如何找出 Telerik 的 RADeditor 默认使用的皮肤?

c# - 如何使用 Math.Max、Math.Min 找到三个最大和三个最小的小数

c# - ASP.NET MVC - RenderAction - PartialView - BeginForm() - 多种形式

c# - 我将如何扩展 WebAPI 以支持通过 HTTP 回调返回 Controller 操作结果?

c# - Selenium c# : Wait Until Element is Present Without Waiting the Time Given, 否则超时

java - 将Unity3D游戏集成到 native Android应用中

c# - 在没有当前 HttpContext 可用的情况下访问 HttpApplicationState

asp.net - 您多久使用一次自定义事件?

@Url.action() 的 ASP.NET MVC 帖子