简体   繁体   English

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

[英]Is there a way to change TimeZone for a request in C# (ASP.NET MVC3)?

Is here a way to change timezone setting for a request, for example, at Application_AcquireRequestState 这是一种更改请求时区设置的方法,例如,在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? 那么对于整个请求,无论何时使用Datetime.ToLocalTime(或新的Datetime()等),本地时间版本都将显示给当前用户/请求?

(or I have to setup timezone for everytime I use ToLocalTime ) (或者我每次使用ToLocalTime都要设置时区)

You need to manage TimeZone info using thread context/storage. 您需要使用线程上下文/存储来管理TimeZone信息。 There is no 1:1 mapping from Culture to TimeZone ( it's a many:many relationship ) ( Reference Post ). 从Culture到TimeZone没有1:1映射( 这是一个多对多的关系 )( 参考帖子 )。 There are many ways to do this - such as using a custom ThreadPrincipal with context members or Thread Local Storage (TLS) 有很多方法可以做到这一点-例如使用带有上下文成员的自定义ThreadPrincipal线程本地存储 (TLS)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM