大网数据广告活动
Web服务器与PHP

Set current culture programmatically in ASP.NET

· 52CE 我爱测

This article describes that how to change the current culture and CurrentUICulture in an ASP.NET application.

This article describes how to change the current culture and current UI culture in an ASP.NET application.

Original product version:   ASP.NET
Original KB number:   306162

Summary

In the .NET Framework, the CultureInfo class from the System.Globalization namespace provides culture-specific information such as the associated language, country/region, calendar, and cultural conventions. The CurrentCulture property represents the culture that the current thread uses. The CurrentUICulture property represents the current culture that Resource Manager uses to look up culture-specific resources at run time. There are three ways to set the Culture information in an ASP.NET application: application level, page level, and thread level.

Application level

Specify application level culture information in the web.config file:

  1. Start Notepad (or any other text editor).

  2. Paste the following code in Notepad:

    <configuration>
        <system.web>
            <globalization
                culture="ja-JP"
                uiCulture="zh-HK"
            />
        </system.web>
    </configuration>
    
  3. Save the text file as web.config in the root folder of your Web server. For example, C:\Inetpub\wwwroot\Web.config.

  4. Start another instance of Notepad. Paste the following code in Notepad:

    <%@Page Language="C#" %>
    <% @Import Namespace="System.Globalization" %>
    <html>
        <head>
        </head>
        <script runat=server>
            public void Page_Load()
            {
                Response.Write ("Current Culture is " + CultureInfo.CurrentCulture.EnglishName);
            }
        </script>
        <body>
        </body>
    </html>
    
  5. Save this text file as Application.aspx in the root folder of your Web server.

  6. Start Internet Explorer, and then open Application.aspx.

Page level

The page level culture-specific information settings override the application level culture-specific information settings. Specify page level culture information by using the <@Page> directive:

  1. Follow steps 1, 2, and 3 of the Application level section to create the web.config file.

  2. Start another instance of Notepad. Paste the following code in Notepad:

    <%@Page Culture="fr-FR" Language="C#" %>
    <% @Import Namespace="System.Globalization" %>
    <html>
        <head>
        </head>
            <script runat=server>
            public void Page_Load()
            {
                Response.Write ("Current Culture is " + CultureInfo.CurrentCulture.EnglishName);
            }
            </script>
        <body>
        </body>
    </html>
    
  3. Save this text file as Page.aspx in the root folder of your Web server.

  4. Start Internet Explorer, and then open Page.aspx.

    [!NOTE] The Current Culture setting is French, although Japanese is specified as the current culture in the web.config file.

Thread level

The thread level culture-specific information settings override the page level culture-specific information settings. Specify the thread level culture-specific information by setting the CurrentCulture property or the CurrentUICulture property of the current thread:

  1. Follow the steps 1, 2, and 3 of the Application level section to create the web.config file.

  2. Start another instance of Notepad. Paste the following code in Notepad:

    <% @Page Culture="fr-FR" Language="C#" %>
    <% @Import Namespace="System.Globalization" %>
    <% @Import Namespace="System.Threading" %>
    <html>
        <head>
        </head>
            <script runat=server>
            public void Page_Load()
            {    // Display the Current Culture
                Response.Write("Current Culture is " + Thread.CurrentThread.CurrentCulture.EnglishName + "<br>");
    
                // Modify the Current Culture
                Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
                Response.Write("Changing Culture to " + Thread.CurrentThread.CurrentCulture.EnglishName + "<br>");
            }
            </script>
        <body>
        </body>
    </html>
    
  3. Save this text file as Thread.aspx in the root folder of your Web server.

  4. Start Internet Explorer, and then open Thread.aspx.

    [!NOTE] The current culture setting is German, although French is specified at the page level and Japanese is specified in the web.config file.

References

For more information, see CultureInfo Class.

Source: Microsoft Learn / IT Pro troubleshooting by Microsoft and contributors. Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / IT Pro troubleshooting
适用范围
See source applicability · Source date 04/07/2020
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。