Use the Profile class to store and set the UI culture
2004-09-21 18:58
603 查看
I got some questions regarding how to use the Profile class to set the current UI culture on each request of a page. In the current version of ASP.Net 1.x, we could use the BeginRequest even in global.asax to set the UI culture. By doing that all pages will use the culture specified in the BeginRequest event. This solution still works in ASP.Net 2.0. But if you have a property in the Profile class that has the culture to be set, the BeginRequest could not be used. The Profile class is not available at that stage.
The Profile feature uses an HttpModule (ProfileModule) to get the profile data for the current user or the default value specified in the web.config.
The ProfileModule will make the Profile class available when the Application’s AcquireRequestState event is executed. The AcquireRequestState is executed on each request of a page.
Note: The AcquireRequestState will be executed after the Profile’s MigrateAnonymous event. So if you change the property of the Profile class in the MigrateAnuinbymouse event when a user is authenticated, the new values will be available in the AcquireRequestState event.
Instead of using the BeginRequest event you can now use the AcquireRequestState event to set the UI culture before the requested page is processed (If you want to store the user’s culture into the Profile class). If the user is not authenticated and you have enabled the support of anonymous users, the default value specified for the properties of the profile will be available for the anonymous user at the AcquireRequestState event.
The following is a simple example that will demonstrate the use of the Profile class and the AcquiredRequestState event to set the UI culture on each request:
Web.config:
<?xml version="1.0"?>
<configuration>
<system.web>
<authentication mode="Forms" />
<profile>
<properties>
<add name="Culture" defaultValue="en-US" type="System.String" allowAnonymous="true"/>
</properties>
</profile>
<anonymousIdentification enabled="true"/>
</system.web>
</configuration>
As you can see in the web.config above there is a default value for the Culture property. When the use is not authenticated the default value specified for the Culture property will be used to set the UI culture.
Global.asax:
void Application_AcquireRequestState(object sender, EventArgs e)
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Profile.Culture);
}
The above code uses the AcquireRequestState event to set the UI culture on each request. The value form the Profile’s Culture property would be the culture. As I mention before, the DeafultValue (en-US in this case) will be used if the user is not authenticated.
If you now authentiacte the user and set a new value to the Profile’s Culture property, e.g. sv-SE for Swedish, the next request made to any of the pages in your web application will use the new Culture for the UI.
Here is an example where the Culture property is set after a user is authenticated:
if (!Request.IsAuthenticated)
{
FormsAuthentication.SetAuthCookie("fredrik", false);
Profile.Culture = "sv-SE";
}
The Profile feature uses an HttpModule (ProfileModule) to get the profile data for the current user or the default value specified in the web.config.
The ProfileModule will make the Profile class available when the Application’s AcquireRequestState event is executed. The AcquireRequestState is executed on each request of a page.
Note: The AcquireRequestState will be executed after the Profile’s MigrateAnonymous event. So if you change the property of the Profile class in the MigrateAnuinbymouse event when a user is authenticated, the new values will be available in the AcquireRequestState event.
Instead of using the BeginRequest event you can now use the AcquireRequestState event to set the UI culture before the requested page is processed (If you want to store the user’s culture into the Profile class). If the user is not authenticated and you have enabled the support of anonymous users, the default value specified for the properties of the profile will be available for the anonymous user at the AcquireRequestState event.
The following is a simple example that will demonstrate the use of the Profile class and the AcquiredRequestState event to set the UI culture on each request:
Web.config:
<?xml version="1.0"?>
<configuration>
<system.web>
<authentication mode="Forms" />
<profile>
<properties>
<add name="Culture" defaultValue="en-US" type="System.String" allowAnonymous="true"/>
</properties>
</profile>
<anonymousIdentification enabled="true"/>
</system.web>
</configuration>
As you can see in the web.config above there is a default value for the Culture property. When the use is not authenticated the default value specified for the Culture property will be used to set the UI culture.
Global.asax:
void Application_AcquireRequestState(object sender, EventArgs e)
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Profile.Culture);
}
The above code uses the AcquireRequestState event to set the UI culture on each request. The value form the Profile’s Culture property would be the culture. As I mention before, the DeafultValue (en-US in this case) will be used if the user is not authenticated.
If you now authentiacte the user and set a new value to the Profile’s Culture property, e.g. sv-SE for Swedish, the next request made to any of the pages in your web application will use the new Culture for the UI.
Here is an example where the Culture property is set after a user is authenticated:
if (!Request.IsAuthenticated)
{
FormsAuthentication.SetAuthCookie("fredrik", false);
Profile.Culture = "sv-SE";
}
相关文章推荐
- Set the Culture and UI Culture for C# Globalization
- Use C# to get JSON Data from the Web and Map it to .NET Class => Made Easy! 转
- Use the Profile Manager to create and remove Firefox profiles
- A c++ class wrapper to simplify the use of CRITICAL_SECTION and avoid dead-lock
- A c++ class wrapper to simplify the use of CRITICAL_SECTION and avoid dead-lock
- CodeIgniter 错误: In order to use the Session class you are required to set an encryption key
- CodeIgniter 错误: In order to use the Session class you are required to set an encryption key
- Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead
- CodeIgniter 错误: In order to use the Session class you are required to set an encryption key
- How to create fully custom Role, User, Event, Resource classes for use with the Security and Scheduler modules
- SCRIPT - to Set the 'SESSION_CACHED_CURSORS' and 'OPEN_CURSORS' Parameters(文档 ID 208857.1)
- ASP.NET MVC Toolkit - how to use the Html Helper class
- PHP executable not found. Install PHP 7 and add it to your PATH or set the php.executablePath s
- HikariCP 脑火Failed to obtain JDBC Connection: You need to run the CLI build and you need target/classes in your classpath to run.
- (Django) How do I set limit_choices_to another class from where the ManyToManyField points to? - django, django-models - TechQues.com
- Eclipse:Could not create the view: Plug-in org.eclipse.jdt.ui was unable to load class org.eclipse.
- Use Microsoft SharePoint Designer 2010 to customize the list view 3---Parameter and Filter
- 关于JAVA界面风格(How to Set the Look and Feel)
- use code to generate Silverlight Controls at run time, and serialize the control into XAML file
- How to get and set the system socket buffer in AIX?