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:
- <System. Web>
- ......
- <Globalization requestencoding = "gb2312"
- Responseencoding = "gb2312" Culture = "ZH-CN"
- Fileencoding = "gb2312"/>
- ......
- </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:
- String name = "Chinese parameter ";
- Response. Redirect ("B. aspx? Name = "+ server. urlencode (name ));
Receive
Code:
- String name = request. querystring ["name"];
- 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:
- <Script language = "JavaScript">
- Function gourl ()
- {
- VaR name = "Chinese parameter ";
- Location. href = "B. aspx? Name = "+ escape (name );
- }
- <Body onclick = "gourl ()">
Receive
Code:
- String name = request. querystring ["name"];
- 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:
- Or use
- Response. Redirect ("test1.aspx? 111 = "+ system. Web. httputility
- . Urlencode ("People's Republic of China "));
- // We recommend that you obtain Chinese parameters from other pages without garbled characters.
- String message =
- "Http: // localhost/test/test1.aspx? 111 =
- "+ System. Web. httputility. urlencode (" People's Republic of China ");
- HTTP:
- // You need to obtain the address of the return value of a page"
- // Send the request
- Httpwebrequest myhttpwebrequest
- = (Httpwebrequest) webrequest. Create (Message );
- // Accept the request
- Httpwebresponse myhttpwebresponse
- = (Httpwebresponse) myhttpwebrequest. getresponse ();
- Stream receivestream =
- Myhttpwebresponse. getresponsestream ();
- Streamreader readstream =
- New streamreader (receivestream,
- System. Text. encoding. getencoding ("gb2312 "));
- // This is the returned result of the return value of the page to be retrieved.
- Returnvalue = readstream. readtoend ();