简体   繁体   中英

What does <%: %> do in ASP.NET (MVC2)?

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?

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.

它会自动将呈现的输出包装在Html.Encode()中以避免脚本攻击。

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 :

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM