Encode the Url. The front-end JS needs to use this content. In this case, it needs to be decoded. Sometimes there may be such a requirement. I use it in the background:
The Code is as follows:
HttpUtility. UrlEncode (str, System. Text. Encoding. UTF8 );
Encode the Url. The front-end JS needs to use this content. In this case, it needs to be decoded:
Code
The Code is as follows:
/**
* Url Encoding
**/
ShengUtils. encode = function (unzipStr ){
Var zipstr = "";
Var strSpecial = "! \ "# $ % & '() * +,/:; <=>? [] ^ '{| }~ % ";
Var tt = "";
For (var I = 0; I Var chr = unzipStr. charAt (I );
Var c = ShengUtils. StringToAscii (chr );
Tt + = chr + ":" + c + "n ";
If (parseInt ("0x" + c)> 0x7f ){
Zipstr + = encodeURI (unzipStr. substr (I, 1 ));
} Else {
If (chr = "")
Zipstr + = "+ ";
Else if (strSpecial. indexOf (chr )! =-1)
Zipstr + = "%" + c. toString (16 );
Else
Zipstr + = chr;
}
}
Return zipstr;
}
/**
* Url Decoding
**/
ShengUtils. decode = function (zipStr ){
Var uzipStr = "";
For (var I = 0; I Var chr = zipStr. charAt (I );
If (chr = "+ "){
UzipStr + = "";
} Else if (chr = "% "){
Var asc = zipStr. substring (I + 1, I + 3 );
If (parseInt ("0x" + asc)> 0x7f ){
UzipStr + = decodeURI ("%" + asc. toString () + zipStr. substring (I + 3, I + 9). toString ());;
I + = 8;
} Else {
UzipStr + = ShengUtils. AsciiToString (parseInt ("0x" + asc ));
I + = 2;
}
} Else {
UzipStr + = chr;
}
}
Return uzipStr;
}
ShengUtils. StringToAscii = function (str ){
Return str. charCodeAt (0). toString (16 );
}
ShengUtils. AsciiToString = function (asccode ){
Return String. fromCharCode (asccode );
}