为什么 Thread.CurrentThread.CurrentCulture 在页面渲染和 HttpModule.PostRequestHandlerExecute 之间发生变化?
我正在创建一个 HttpModule,它需要知道 MVC 应用程序中设置的 Thread.CurrentThread.CurrentCulture 的值。该值当前由 BaseController 设置,但是当我的 HttpModule.PostRequestHandlerExecute() 方法触发时,它会恢复为页面渲染之前的文化。
我通过使用以下步骤创建一个简单的 Web 应用程序来复制此内容:
- Module.PreRequestHandlerExecute:将区域性设置为 A
- Page_Load:区域性当前为 A。将区域性设置为 B
- Module.PostRequestHandlerExecute:当前线程区域性为 A。我希望它是 B,但是它在页面渲染和 PostRequestHandlerExecute 之间发生了变化
知道为什么 .Net 会改变这个值或者我如何绕过它吗?线程是相同的,因此 .Net 中的某些内容必须显式恢复区域性。
I'm creating an HttpModule that needs to know the value of Thread.CurrentThread.CurrentCulture as set in an MVC application. That value is currently being set by the BaseController, but when my HttpModule.PostRequestHandlerExecute() method fires, it reverts to what the Culture was prior to page rendering.
I have duplicated this by creating a simple web app with these steps:
- Module.PreRequestHandlerExecute: Set culture to A
- Page_Load: Culture is currently A. Set culture to B
- Module.PostRequestHandlerExecute: Current thread culture is A. I expected it to be B but it was changed between page rendering and PostRequestHandlerExecute
Any idea why .Net changes this value or how I could get around it? The thread is the same, so something in .Net must be explicitly reverting the culture.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您只是为正在运行的线程设置区域性,则任何导致线程切换的操作(例如 ASP.NET 中页面生命周期的另一部分)都将导致恢复为默认区域性。
推荐的方法在这里...
http://msdn.microsoft.com/ en-us/library/bz9tc508.aspx
此页面讨论 3 个选项...
值得注意的是,任何模块都是作为页面请求的一部分加载的,因此更改页面级别的区域性应该会更改该请求上所有模块的区域性。
If you simply set the culture for a running thread any operation that results in a thread switch (such as another part of the page lifecycle in asp.net) would result in reversion to the default culture.
The recommended approach is here ...
http://msdn.microsoft.com/en-us/library/bz9tc508.aspx
This page discusses 3 options ...
It's worth noting that any modules are loaded as part of a page request so changing the culture at page level should change it for all modules on that request.