Finally understand the two coding of the use of the scene, to share under. one, HTML encoding
When a label in an HTML page wants to display <a>test</a>, if you simply write label.html ("<a>test</a>"), the page displays a label with the content test.
This is because <, > is a special character in HTML, parsing will be considered HTML tags, at this time to display a page on a, you need to HTML encoding, < escape for < on OK ~
Provides a method for HTML encoding and decoding of JS
function HTMLEncode (s) {
var div = document.createelement (' div ');
Div.appendchild (document.createTextNode (s));
return div.innerhtml;
}
function HtmlDecode (s) {
var div = document.createelement (' div ');
div.innerhtml = s;
return div.innertext;
}
In addition, when using MVC, @Model. Name is already HTML coded for this field, so this is generally not a problem.
However, note that if you want to display in the text Vlaue, in fact, do not need to Html encoding, at this time need to use @Html. Raw(model.name) This form to get the original content. Second, URL coding
URL encoding is a format that browsers use to package form input. The browser takes all the name and the values from the form, encodes them as a name/value parameter (removing those that cannot be transferred, ranking the data, and so on) as part of the URL or sending it to the server in isolation.
Any special characters (that is, those that are not simple seven-bit ASCII, such as Chinese characters) will be encoded in percent hex, including, of course, like =,&;, and% these special characters. In fact, the URL encoding is a character ASCII hex. However, there are some changes that need to be preceded by the words "%".
Above from Baidu Encyclopedia
Give a use of the scene, such as the page jump parameters,? a= Chinese +, if directly redirected to this link, + special characters will be filtered out, this time need to do URL coding
JS with the URL encoding method, encodeURI (), encodeURIComponent ()
Corresponding URL decoding method, decodeURI (), decodeURIComponent ()
The method of URL encoding in btw:c#
Encoding: Server.URLEncode (String)
Decoding: Server.urldecode (String)