views:

1334

answers:

4

What is meant by HTMLEncode in ASP.net. How we use this HTMLEncode to controls. What is meant by urlEncode and in what way it differenitates from HTMLEncode?

+1  A: 

HTMLEncode and URLEncode deal with invalid characters in HTML and URLs, or more accurately, characters that need to be specially written to be interpreted correctly. For example, in HTML the < and > characters are used to indicate tags. Thus, if you wanted to write a math formula, something like 1+1 < 2+2, the '<' would normally be interpreted as the beginning of a tag. HTMLEncoding turns this character into "&lt;" which is the encoded representation of the less-than sign. URLEncoding does the same, but for URLs, for which the special characters are different, although there is some overlap.

Michael Bray
+6  A: 

urlEncode replaces special characters with characters that can be understood by web browsers/web servers for the purpose of addressing... hence URL. For instance, spaces are replaced with %20, ' = %27 etc...

See these references:

HtmlEncode replaces special characters with character strings that are recognised by the HTML engine itself to render the content of the page - things like & becomes &amp; or < = &lt; > = &lt; this prevents the HTML engine from interpreting these characters as parts of the HTML markup and therefore render them as if they were strings.

See this reference:

BenAlabaster
+3  A: 

Both HTML and URL's are essentially very constrained languages. As a language they add meaning to specific keywords or operators. For both of these languages though, keywords are almost always single characters. For example

  • HTML: > and <
  • URL: / and :

In the use of each language though it is possible to use these constructs in a manner that does not ensure the meaning of the language. For instance this post contains a > character. I do not want it to be interpreted as HTML, just text.

This is where Encode and Decode methods come into play. These methods will respectively take a string and convert any of the characters that would otherwise be treated as keywords into an escaped form which will not be interpreted as part of the language.

For instance: Passing > into HtmlEncode will return &gt;

JaredPar
Beautiful articulation!
Quintin Par
A: 

Basically its safe to say that when you deal with URLs and Query Strings Use URLEncode and when you deal with HTML of a page, use HTMLEncode