Sdílet prostřednictvím


How to: Select an Encoding for ASP.NET Web Page Globalization

Internally, the code behind ASP.NET Web pages handles all string data as Unicode. You can set how the page encodes its response, which sets the CharSet attribute on the Content-Type part of the HTTP header. This enables browsers to determine the encoding without a meta tag or having to deduce the correct encoding from the content. You can also set how the page interprets information that is sent in a request.

Finally, you can set how ASP.NET interprets the content of the page itself — in other words, the encoding of the physical .aspx file on disk. If you set the file encoding, all ASP pages must use that encoding. Notepad.exe can save files that are encoded in the current system ANSI codepage, in UTF-8, or in UTF-16 (also called Unicode). The ASP.NET runtime can distinguish between these three encodings. The encoding of the physical ASP.NET file must match the encoding that is specified in the file in the @ Page encoding attributes.

Note

Some designers, such as Visual Studio 2005, can save .aspx files that use different encodings.

Warning

The name of the Web application might not display correctly on a user's computer, if the name uses a double-byte character set (DBCS) that is not the language of the user's operating system. On earlier versions of Microsoft FrontPage server extensions and on Microsoft Internet Information Services (IIS) versions 5.1 and earlier, the project name must also match the language of the operating system for the Web server in order to display correctly.

To specify encoding

  • To set the encoding for all pages, add a Globalization property to the Web.config file, and then set its fileEncoding, requestEncoding, and responseEncoding attributes, as shown in the following example:

    <configuration>
      <system.web>
        <globalization
          fileEncoding="utf-8"
          requestEncoding="utf-8"
          responseEncoding="utf-8"
          culture="en-US"
          uiCulture="de-DE"
        />
      </system.web>
    </configuration>
    
  • To set the encoding for an individual page, set the RequestEncoding and ResponseEncoding attributes of the @ Page directive, as shown in the following example:

    <%@ Page RequestEncoding="utf-8" ResponseEncoding="utf-8" %>
    

    Note

    You cannot set the fileEncodingattribute, because it applies to the file itself.

See Also

Other Resources

ASP.NET Globalization and Localization