Use the Profile class to store and set the UI culture

 

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";

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值