C #:
[Csharp]
String name = "Chinese ";
// Compile to url format
Name = System. Web. HttpContext. Current. Server. UrlEncode (name );
String name = "Chinese ";
// Compile to url format
Name = System. Web. HttpContext. Current. Server. UrlEncode (name );
Javascript:
[Javascript]
Var name = "Chinese ";
// Compile to url format
Name = encodeURI (name );
Var name = "Chinese ";
// Compile to url format
Name = encodeURI (name );
There are several methods in javascript to encode URL strings: escape (), encodeURI (), and encodeURIComponent (). These encodings play different roles.
Escape () method:
The specified string is encoded using the ISO Latin character set. All space characters, punctuation marks, special characters, and other non-ASCII characters will be converted into character encoding in % xx format (xx equals to this character in the character set table
Encoded hexadecimal number ). For example, the space character is encoded as % 20. Characters not encoded by this method: @ */+
EncodeURI () method:
Convert a URI string to an escape string in UTF-8 encoding format. Characters not encoded by this method :! @ # $ & * () = :/;? +'
EncodeURIComponent () method:
Convert a URI string to an escape string in UTF-8 encoding format. Compared with encodeURI (), this method will encode more characters, such. So if the string contains several
This method cannot be used for encoding. Otherwise, the URL will display an error after the/character is encoded. Characters not encoded by this method :! *()'
Therefore, for a Chinese string, if you do not want to convert the string encoding format to the UTF-8 format (for example, when the charset of the original page and the target page is consistent), you only need to use escape. If your page is
GB2312 or other encoding, and the page that receives the parameter is UTF-8 encoding, you need to use encodeURI or encodeURIComponent.
In addition, encodeURI/encodeURIComponent is introduced after javascript1.5, and escape is available in javascript1.0.
// Resolution
[Csharp]
String name = Request. Params ["name"]. ToString (). Trim ();
Name = System. Web. HttpContext. Current. Server. UrlDecode (name );
String name = Request. Params ["name"]. ToString (). Trim ();
Name = System. Web. HttpContext. Current. Server. UrlDecode (name );