Solve Chinese garbled characters in ASP. NET

Source: Internet
Author: User

Program developers must have encountered such a problem, that is, when developing ASP. NET, the page may be garbled. The following information is collected online. For more information, see:

There are generally three methods to solve Chinese garbled characters in ASP. NET:

ASP. NET Chinese garbled Solution

1. Set the Web. config file

Code:
  1. <System. Web>
  2. ......
  3. <Globalization requestencoding = "gb2312"
  4. Responseencoding = "gb2312" Culture = "ZH-CN"
  5. Fileencoding = "gb2312"/>
  6. ......
  7. </System. Web>

ASP. NET Chinese garbled solution 2. encode the Chinese parameters to be passed before passing the Chinese characters and decode them upon receiving them.

Transfer

Code:
  1. String name = "Chinese parameter ";
  2. Response. Redirect ("B. aspx? Name = "+ server. urlencode (name ));

Receive

Code:
  1. String name = request. querystring ["name"];
  2. Response. Write (server. urldecode (name ));

ASP. NET Chinese garbled solution 3. If a Chinese parameter is transferred from the. html file to the. aspx file (that is, URL conversion is not performed from the background using the Redirect () method ). Similarly, the passed Chinese parameters must be encoded and decoded upon receiving.

Transfer

Code:
  1. <Script language = "JavaScript">
  2. Function gourl ()
  3. {
  4. VaR name = "Chinese parameter ";
  5. Location. href = "B. aspx? Name = "+ escape (name );
  6. }
  7. <Body onclick = "gourl ()">

Receive

Code:
  1. String name = request. querystring ["name"];
  2. Response. Write (server. urldecode (name ));

Summary of ASP. NET Chinese garbled solutions:

In general. Set the Web. config file. However, if you use JavaScript to call the WebService method (passing Chinese parameters to WebService ). The Web. config file is invalid.

Code:
  1. Or use
  2. Response. Redirect ("test1.aspx? 111 = "+ system. Web. httputility
  3. . Urlencode ("People's Republic of China "));
  4. // We recommend that you obtain Chinese parameters from other pages without garbled characters.
  5. String message =
  6. "Http: // localhost/test/test1.aspx? 111 =
  7. "+ System. Web. httputility. urlencode (" People's Republic of China ");
  8. HTTP:
  9. // You need to obtain the address of the return value of a page"
  10. // Send the request
  11. Httpwebrequest myhttpwebrequest
  12. = (Httpwebrequest) webrequest. Create (Message );
  13. // Accept the request
  14. Httpwebresponse myhttpwebresponse
  15. = (Httpwebresponse) myhttpwebrequest. getresponse ();
  16. Stream receivestream =
  17. Myhttpwebresponse. getresponsestream ();
  18. Streamreader readstream =
  19. New streamreader (receivestream,
  20. System. Text. encoding. getencoding ("gb2312 "));
  21. // This is the returned result of the return value of the page to be retrieved.
  22. Returnvalue = readstream. readtoend ();

 

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.