Recipe 17.2. Localizing RequestResponse Encoding

Recipe 17.2. Localizing Request/Response Encoding


You are developing an application for a specific region, and you want to tell the browser which character set to use in rendering the page.


Set the requestEncoding and responseEncoding attributes of the <globalization> element in web.config to the desired character set:

 <system.web>        <globalization requestEncoding="iso-8859-1" responseEncoding="iso-8859-1" />     </system.web> 


The HTTP header returned to the browser in response to a request contains information not displayed but, nevertheless, controls how the browser displays the content it receives. Included in the header is information that specifies which character set has been used to encode the response data and, by implication, which character set the browser should use to display it.

The request and response encoding information for a page request can be viewed by setting the trace attribute of the @ Page directive to true and viewing the Request Details section.

ASP.NET lets you specify the character set used to encode the response data using the responseEncoding attribute of the <globalization> element in the web.config file, as shown earlier. The responseEncoding attribute can be set to any valid character set. Table 17-1 lists some of the more common character sets used for European languages (English, French, German, and others).

Table 17-1. Common character sets

Character set name



Commonly called Latin 1; covers the Western European languages


Commonly called Latin 2; covers the Central and Eastern European languages


Windows version of the character set covering the Western European languages


Technically not a character set but an encoding scheme to provide the ability to encode Unicode characters as a sequence of bytes

The character set information provided in the response headers is not guaranteed to be honored because the client machine may not have the suggested character set installed. If Internet Explorer is being used, it will prompt the user to install the required character set. If the character set is not installed, IE will use the character set defined by the computer's region locale setting. Other browsers may respond differently.

The requestEncoding attribute is used to specify the assumed encoding for incoming requests. This includes posted data and data passed in the URL. Generally, the requestEncoding attribute is set to the same character set as the responseEncoding attribute.

If your web.config file does not contain a <globalization> element with responseEncoding and requestEncoding attributes set to a particular character set, the values defined in the machine.config file are used instead. By default, requests and responses are encoded in utf-8 format. If your machine.config file does not contain a <globalization> element with the responseEncoding and requestEncoding attributes set to the given character set, the encoding defaults to the computer's region locale setting.

The character set can be defined on a page-by-page basis by placing the <meta> tag shown here in the header section of the HTML (or .aspx) file:

 <meta http-equiv="Content-Type" content="text/html;  charset=iso-8859-1"> 

This approach is not recommended, though, for two reasons. First, you need to add a <meta> tag to every page of your application, which implies some associated maintenance should any changes be required. Second, some browsers interpret <meta> tags only after they have rendered a page, which can cause the page to be rendered twice.

See Also

The "<globalization> Element" in the MSDN Library

ASP. NET Cookbook
ASP.Net 2.0 Cookbook (Cookbooks (OReilly))
ISBN: 0596100647
EAN: 2147483647
Year: 2003
Pages: 202

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: