Home   |   QuickStart Welcome   |   ASP.NET   |   Web Services   |   How Do I...?   
  |   I want my samples in...      

ASP.NET 2.0 Quickstart Tutorials

Encoding

Like other components in the .NET Framework ASP.NET processes strings internally as Unicode, more specifically its 16-bit encoding form UTF-16. Most web protocols are byte-based though which is why requests from and responses to browsers are converted to the byte-based form of Unicode UTF-8 by default. This conversion is algorithmic and pretty much has no performance impact. Most browsers these days support UTF-8 encoded pages. For older browsers or browsers on mobile devices it might be necessary though to convert the communication with the browser to one of the legacy codepage-based encodings. The following sample shows how to configure for example the Japanese encoding Shift-JIS in the globalization section of a web.config file.

In this case web develop might also want to save their files in the codepage encoding. This can be declared using the fileEncoding attribute.

<configuration>
  <system.web>
    <globalization
       requestEncoding="shift-jis"
       responseEncoding="shift-jis"
       fileEncoding="shift-jis"
       ...
    />
  </system.web>
</configuration>
Both requestEncoding and responseEncoding should typically be set to the same value. These configuration settings except for fileEncoding can also be set in the @ Page declaration. The reason of course is that the directive is in the page file and the file encoding has to be known when the page is begin to be read.

If no fileEncoding declaration is available the ASP.NET runtime is determining the file encoding by detecting any Unicode signatures at the begin of the file and is using these to distinguish between UTF-8 and UTF-16 encoded pages. Unicode signatures are added by Visual Studio and Notepad automatically when saving a file as UTF-16, Notepad also adds a signature for UTF-8 and it can be specified in Visual Studio. If no signature is present the runtime will interpret the source file in the current system ANSI codepage of the system the page is run on. The recommendation is to always save files in Unicode with a signature.