Asp.net UTF-8 encoding Chinese garbled Solutions

Source: Internet
Author: User
ArticleDirectory
    • Summary

Asp.net UTF-8 encoding Chinese garbled Solutions

From http://hi.baidu.com/datachina/blog/item/d41c372e8671cf564ec22679.html

Unicode encoding is gradually becoming the most common solution supported by multiple languages. Unicode-Encoded chinese web pages are compatible with various platforms and browsers. UTF-8 is a storage/exchange implementation method of Unicode. Unicode codes of different numeric ranges are encoded in a variable length mode. All ASCII characters occupy 1 byte, and those greater than 0x7f occupy 2 to 4 bytes. We can see that all ASCII files are directly compatible with UTF-8. In additionSource codeIn this case, files with a large portion of ASCII characters are typically more space-saving than other Unicode storage/Interchange Formats (such as UTF-16, utf-32, and so on. Therefore, UTF-8 is widely used in website design.

However, when using UTF-8 encoding in the Asp.net Chinese webpage, a slight carelessness may cause Chinese garbled characters, which is a headache. We recommend that you use gb2312 encoding wherever necessary. This is obviously not a complete solution. This article discusses how to fully use UTF-8 encoding in the Asp.net website.

Create an Asp.net site in Visual Studio 2005. In the Web. config file, set the site to use UTF-8 encoding:

<? XML version = "1.0"?> ... <Configuration> <system. Web> <globalization fileencoding = "UTF-8"/> </system. Web> <configuration>...

In this way, VS can implement automated support for UTF-8 to a certain extent, but it is not perfect. Garbled characters often occur. The following discussion can effectively solve Chinese garbled characters even if no site code is set.

Create an Asp.net page and writeCodeAs follows:

<% -- Sample. aspx -- %> <% @ page Language = "C #" autoeventwireup = "true"   Codefile = "sample. aspx. cs" inherits = "sample" %> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml"> Test </Title>  Old days </ASP: listitem> <asp: listitem> http://live.mingliang.org </ASP: listitem> </ASP: radiobuttonlist> <asp: button id = "button1" runat = "server" text = "Submit" onclick = "button#click"/> <br/> <asp: label id = "label1" runat = "server"> </ASP: Label> </form> </body> 

By default, the page created in Visual Studio 2005 is gb2312 encoding. Next, convert the webpage to UTF-8 encoding. Microsoft Expression web tool is recommended here. The predecessor of this software is the well-known FrontPage. After being updated to expresson web, its functions have been greatly increased, such as enhanced CSS support, perfect support for Asp.net controls, and even master pages. Therefore, it can be said that it is used with Visual Studio 2005 and is the preferred tool for editing the Asp.net webpage.

Open the created sample in expression web. aspx, right-click page properties in the design view, set Save the document as Unicode (UTF-8) in the language tag, OK, save. In this way, expression web automatically adds the file description encoding to the source file.<Meta>And convert the files stored on the hard disk to UTF-8 format.

Opening it in vs generates garbled characters:

This is because the UTF-8 encoding is not detected by default in Vs, and the file is still interpreted in gb2312 mode. There are two solutions. First, enable the UTF-8 mandatory check in the tools> Options menu of:

The second is to add the BOM (byte-order mark) mark to the source code of the webpage as the Unicode encoding signature (RecommendedBecause this ensures that the file encoding can be correctly detected at any time ). Open the aspx file with ultraedit, click File-> Save As, select the UTF-8 in the format drop-down box in the SAVE dialog box, overwrite the original file name to save. In hexadecimal editing mode, you can see that the file has been marked with a three-byte BOM:

Of course, in the vs Save As dialog box, click the drop-down menu on the right of the Save button, and save in UTF-8 with signature way can also achieve the same effect.

In this way, the Chinese Asp.net webpage is saved in UTF-8 mode and can be browsed normally. The following code is used to compile a button for the webpage:

 
Using system; public partial class sample: system. web. UI. page {protected void page_load (Object sender, eventargs e) {} protected void button#click (Object sender, eventargs e) {label1.text = "you selected:" + radiobuttonlist1.selectedvalue ;}}

Run the webpage, click submit, and garbled again!

What's even more strange is that it would be good to see it in the vs debugger. The following clues can be found: the source code of Asp.net is encoded in the same way as that of the source file. Therefore, in order to make the strings in the source code can be properly displayed and processed, you need to change the storage mode of the source files to UTF-8. This time, you can simply save the source files to the UTF-8 format using vs or ultraedit, remember to save the BOM by the way. In this way, the page display is completely normal.

Summary

If UTF-8 encoding is used in the Chinese Asp.net, you need to do the following:

    1. HTML code to add the definition file encoding<Meta>Mark
    2. . Aspx FileAnd the corresponding source code fileThe physical storage format of must be converted to UTF-8
    3. We recommend that you add the BOM header to the code file to clearly indicate its physical format.
    4. Specify the website code in the web. config file to avoid Chinese garbled characters to some extent.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.