ASP.NET 使用后重置线程文化?

发布于 2024-09-19 23:48:46 字数 112 浏览 6 评论 0原文

如果我为一个 ASPX 设置线程文化和 UICulture,那么在传递该页面后,所有使用相同线程(不是相同请求)的 aspx 将具有相同的文化?

因为我需要为一个 ASMX 设置 Culture

If I set Thread Culture and UICulture for one ASPX, after pass for that page, all my aspx that use the same thread(not same request) will have the same Culture?

Because I need to set Culture just for one ASMX

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

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

发布评论

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

评论(2

安稳善良 2024-09-26 23:48:46

如果您设置的区域性不是从浏览器设置中读取的(就像它存在于数据库中一样),您需要在每个请求上进行设置。

正如这里所描述的:
http://msdn.microsoft.com/en-us/library/bz9tc508。 aspx

在每个页面上覆盖页面的“InitializeCulture”方法。所有页面的通用基类在这里非常方便。

我建议启动 .NET 反射器并查看默认实现的作用。这将有助于澄清默认情况下发生的情况。

由于此事件是在页面级别处理的,而不是在 Global.asax 中处理,因此我希望它会被重新设置。此外,正如本文所述,此事件在页面生命周期的早期被调用,捕获用户输入需要直接访问“Request.Form”。

编辑:请尝试此操作,并确保必须在每个请求中设置此操作。如果您看到不同的结果或者我误解了您的问题,请告诉我。

Default.aspx:打印“21.09.2010”

<%@ Page Language="C#" %>

<%@ Import Namespace="System.Threading" %>
<%@ Import Namespace="System.Globalization" %>

<script runat="server">
protected override void InitializeCulture()
        {
            UICulture = "de-DE";
            Culture = "de-DE";
            //base.InitializeCulture();
        }   
</script>
<HTML>
<head>
</head>
<body>
<%= System.DateTime.Now.ToShortDateString()%>
</body>
</HTML>

Default2.aspx:打印“9/21/2010”(我的默认语言是 es-US)

<%@ Page Language="C#" %>
<HTML>
<head>
</head>
<body>
<%= System.DateTime.Now.ToShortDateString()%>
</body>
</HTML>

您点击这些页面的顺序并不重要。结果没有改变。

人们使用的一种方法是将这些信息存储在会话变量中,并使用会话变量来设置文化。因此,其逻辑是集中的。

If the culture you set is not read from the browser settings (like it lives in a database) You need to set this on every request.

As described here:
http://msdn.microsoft.com/en-us/library/bz9tc508.aspx

Override the page's 'InitializeCulture' method on every page. A common base class for all your pages comes in really handy here.

I would suggest firing up .NET reflector and see what the default implementation does. It will help clarify what is going on by default.

As this is event is handled on the page level, and not in the Global.asax, I would expect this is re-set. also, as the article describes, this event is Called so early in the page life cycle that capturing User Input requires directly accessing 'Request.Form'.

EDIT: Please try this and See that this must be set in every request. Let me know if you see different results or if I misunderstand your question.

Default.aspx: prints '21.09.2010'

<%@ Page Language="C#" %>

<%@ Import Namespace="System.Threading" %>
<%@ Import Namespace="System.Globalization" %>

<script runat="server">
protected override void InitializeCulture()
        {
            UICulture = "de-DE";
            Culture = "de-DE";
            //base.InitializeCulture();
        }   
</script>
<HTML>
<head>
</head>
<body>
<%= System.DateTime.Now.ToShortDateString()%>
</body>
</HTML>

Default2.aspx: prints '9/21/2010' (my default cluture is es-US)

<%@ Page Language="C#" %>
<HTML>
<head>
</head>
<body>
<%= System.DateTime.Now.ToShortDateString()%>
</body>
</HTML>

The order in which you hit these pages does not matter. The results do not change.

One approach people is used is to store this info in a Session variable and use the Session Variables to set the culture ..so logic for this is centerlized.

晌融 2024-09-26 23:48:46

我非常确定 UICulture 一旦设置,就会在整个 ASP 会话中保留(这独立于您为自己的应用程序创建的任何会话而发生)。

编辑:看起来像一个简单的总结: http://quickstarts.asp .net/QuickStartv20/aspnet/doc/localization/localization.aspx

I'm pretty sure that UICulture, once set, stays for the entire ASP session (which happens independently of whatever session you create for your own application).

Edit: looks like a straightforward summary here: http://quickstarts.asp.net/QuickStartv20/aspnet/doc/localization/localization.aspx

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