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

yuzhangqi發表於2010-09-16

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 encoding attributes.

To specify encoding

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



fileEncoding="utf-8"
requestEncoding="utf-8"
responseEncoding="utf-8"
culture="en-US"
uiCulture="de-DE"
/>

To set the encoding for an individual page, set the RequestEncoding and ResponseEncoding attributes of the @ Page directive, as shown in the following example:

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

[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/13651903/viewspace-1038445/,如需轉載,請註明出處,否則將追究法律責任。

相關文章