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
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>
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 covering the Western European languages |
utf-8 |
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 header 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
as well as 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 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.