Saving Chinese and Unicode Characters in ASP.NET

 

There are a lot of articles about this on the net:

http://csharpindepth.com/Articles/General/Strings.aspx
http://www.joelonsoftware.com/articles/Unicode.html

But suffice to say this. When you save characters from a form postback the characters ***will*** be encoded. If you do not specify an encoding it will be determined for you.

This can be very confusing!!!

Chinese characters may appear to look OK in ASP but not in ASP.NET

So to stop problems, always declare your encodings! Typically we use utf-8.

In ASP, do this:

@CodePage = 65001
Option Explicit
Session.CodePage = 65001
Response.CodePage = 65001
Response.CharSet = "utf-8"

When saving SQL directly, make sure the characters get saved in nvarchar, ntext etc

Declare the encoding in HTML as well:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

In ASP.NET, add this to web.config:

globalization
requestEncoding="utf-8"
responseEncoding="utf-8"
fileEncoding="utf-8"
Share