6

A coworker recently checked in a changeset where lots of expressions on the form <%= (...) %> were changed to <%: (...) %>.

I have a vague recollection of having heard what <%: does, but cannot remember. It's too late in the night to call my coworker, and Google and Bing both seem unable to search for the string "<%:".

Can someone enlighten me, please?

Tor Haugen
  • 19,509
  • 9
  • 45
  • 63
  • related, but doesn't cover this construct - http://stackoverflow.com/questions/160097/whats-the-difference-between-and – ChrisF Jul 25 '10 at 23:14

3 Answers3

12

It HtmlEncodes the string, if it hasn't already been encoded.

The "hasn't already been encoded part" is why MvcHtmlString was introduced. MVC2 returns MvcHtmlString from many HtmlHelper methods to represent strings that should not be re-encoded. The <%: %> knows not to re-encode.

See What is an MvcHtmlString and when should I use it? for a good discussion.

Community
  • 1
  • 1
Rob
  • 5,525
  • 1
  • 24
  • 26
8

It automatically wraps the rendered output in Html.Encode() to avoid scripting attacks.

Tahbaza
  • 9,486
  • 2
  • 26
  • 39
1

One way to remember this is (courtesy: Scott Hanselman) think = as an closed gate. Now open the gate by turning it 90 degrees and see it from the same view. you will see :

Subhasis
  • 714
  • 3
  • 14
  • 28