14.1. Localizing Request/Response Encoding

Problem

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.

Solution

Set the requestEncoding and responseEncoding properties of the <globalization> element in web.config to the desired character set, as shown here:

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

Discussion

The HTTP header returned to the browser in response to a request contains information that is 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.

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 14-1 lists some of the more common character sets used for Latin-based languages (English, French, German, and others).

Table 14-1. Common character sets

Character set name

Description

iso-8859-1

Commonly called Latin 1 and covers the Western European languages

iso-8859-2

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

Windows-1252

Windows version of the character set ...

Get ASP.NET Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.