ASP.NET reset thread culture after use?
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
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.
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
精彩评论