有没有办法更改 C# (ASP.NET MVC3) 中请求的时区?

发布于 2024-12-20 09:02:20 字数 210 浏览 6 评论 0原文

这里是否有一种方法可以更改请求的时区设置,例如,在 Application_AcquireRequestState 处,

然后对于整个请求,无论我何时使用 Datetime.ToLocalTime (或 new Datetime() 等)、本地时间版本将显示给当前用户/请求?

(或者我每次使用 ToLocalTime 时都必须设置时区)

Is here a way to change timezone setting for a request, for example, at Application_AcquireRequestState

then for the whole request, no matter when I use Datetime.ToLocalTime (or new Datetime() etc), a local time version will be shown to current user/request?

(or I have to setup timezone for everytime I use ToLocalTime )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

王权女流氓 2024-12-27 09:02:20

您需要使用线程上下文/存储来管理时区信息。从文化到时区不存在 1:1 映射(这是一种多:多关系)(参考帖子< /a>)。有很多方法可以做到这一点 - 例如使用自定义 ThreadPrincipal 与上下文成员或 线程本地存储 (TLS)

You need to manage TimeZone info using thread context/storage. There is no 1:1 mapping from Culture to TimeZone (it's a many:many relationship) (Reference Post). There are many ways to do this - such as using a custom ThreadPrincipal with context members or Thread Local Storage (TLS)

情深缘浅 2024-12-27 09:02:20

在视图中重写 InitializeCulture() 并将 Thread.CurrentThread.CurrentCulture 和 Thread.CurrentThread.CurrentUICulture 属性设置为您选择的区域性。

Override InitializeCulture() in your view and set the Thread.CurrentThread.CurrentCulture and Thread.CurrentThread.CurrentUICulture properties to the culture of your choice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文