简体   繁体   English

ASP.NET(MVC2)中<%:%>做了什么?

[英]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 "<%:". 打电话给我的同事已经太晚了,Google和Bing似乎都无法搜索字符串“<%:”。

Can someone enlighten me, please? 有人可以开导我吗?

It HtmlEncodes the string, if it hasn't already been encoded. 它HtmlEncodes字符串,如果尚未编码。

The "hasn't already been encoded part" is why MvcHtmlString was introduced. “还没有被编码的​​部分”是MvcHtmlString被引入的原因。 MVC2 returns MvcHtmlString from many HtmlHelper methods to represent strings that should not be re-encoded. MVC2从许多HtmlHelper方法返回MvcHtmlString来表示不应重新编码的字符串。 The <%: %> knows not to re-encode. <%:%>知道不重新编码。

See What is an MvcHtmlString and when should I use it? 请参阅什么是MvcHtmlString以及何时使用它? 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. 现在打开门将其旋转90度并从同一视图中看到它。 you will see : 你会看见 :

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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