Visual Studio UTF-8 code complete Chinese garbled Solution

Source: Internet
Author: User
Tags ultraedit

Unicode encoding is gradually becoming the most common solution supported by multiple languages. Unicode
The Encoded chinese webpage is compatible with various platforms and browsers. UTF-8 is a storage/exchange implementation method of Unicode. For values in different ranges
Unicode code, which is 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 addition, for files with a large portion of the ASCII characters such as the source code of a webpage, it is usually more
Storage/exchange formats (such as UTF-16, utf-32, etc.) are more space-saving. Therefore, UTF-8 is widely used in website design.

However, in the Asp.net Chinese webpage, UTF-8 is used.
When encoding, a slight carelessness will cause Chinese garbled characters, causing headaches. For this problem, we recommend that you use gb2312 wherever necessary.
Encoding. 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
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 UTF-8 to a certain extent.
Automation support, but not very complete. 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 write the following code:

<% -- 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> Xmlns = "http://www.w3.org/1999/xhtml"> <Title> test </title> Id = "form1" runat = "server"> <asp: radiobuttonlist
Id = "radiobuttonlist1" runat = "server"> <asp: listitem> 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>

Visual
By default, the page created in Studio 2005 is still gb2312 encoding. Next, convert the webpage to UTF-8 encoding. Microsoft is recommended.
Expression Web
Tool. The predecessor of this software is the well-known FrontPage. When updating to expresson Web
After that, its functions were greatly increased, such as enhanced support for CSS, perfect support for Asp.net controls and even master pages. So it can be said that it is
Visual Studio 2005 is used together to edit the preferred tool for the Asp.net webpage.

Open sample. aspx in expression web and right-click the page in design view.
Properties, set Save the document as Unicode in the Language Label
(UTF-8), OK, save. In this way, expression web automatically adds the file description encoding to the source file.

<Meta>

Mark
And convert the files stored on the hard disk to UTF-8 format.

Opening it in vs generates garbled characters:

 

This is because
By default, UTF-8 encoding is not detected. The file is still interpreted in gb2312 mode. There are two solutions. First, in vs tools->
In the options menu, enable the UTF-8 mandatory check:

 

The second is to add the BOM (byte-order mark) mark to the source code of the webpage as the Unicode encoding signature (Push
Recommended

Because this ensures that the file encoding can be correctly detected at any time ). Use ultraedit to open the aspx file and click File.
-> Save As, select in the format drop-down box of the Save dialog box
UTF-8 to overwrite the original file name. In hexadecimal editing mode, you can see that the file has been marked with a three-byte BOM:

 

Of course, in the Save As dialog box in Vs, click the drop-down menu on the right of the Save button and use the UTF-8 with signature
Save in the same way.

In this way, the Chinese Asp.net webpage uses UTF-8
And can be viewed 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 more strange is that
In the debugger. You can find the following clues by checking relevant information: Asp.net
After the source code is compiled, the encoding method is the same as that of the source file itself. Therefore, in order to make the strings in the source code can be properly displayed and processed, you need to change the source file storage method
UTF-8. This time, you can use vs or ultraedit to directly save it as UTF-8. Remember to choose BOM
. 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 File
    And the physical storage format of the corresponding source code file needs to 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. In
    The website code is specified in the web. config file, which can avoid Chinese garbled characters to some extent.
Related Article

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.