有没有办法更改 C# (ASP.NET MVC3) 中请求的时区?
这里是否有一种方法可以更改请求的时区设置,例如,在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用线程上下文/存储来管理时区信息。从文化到时区不存在 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)
在视图中重写 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.