MVC2<%:标签与<%=有何不同 [英] MVC2 how is <%: tag different to <%=

查看:65
本文介绍了MVC2<%:标签与<%=有何不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<%:和<%=吗?

what is the difference between <%: and <%= ?

推荐答案

区别在于<%:HTML自动对字符串进行编码,而<%=则不.

The difference is that <%: automatically HTML encodes the string whereas <%= does not.

返回MVC2以便对字符串进行HTML编码之前,您必须在视图内部使用Html.Encode()方法.

Back before MVC2 came out in order to HTML encode a string you had to use the Html.Encode() method inside the view.

<%= Html.Encode(Model.MyString) %>

但是,他们在MVC2中添加了<%:标记,该标记可以输出相同的内容,但可以为您处理HTML编码.

However with MVC2 they added the <%: tag, which outputs the same thing but handles the HTML encoding for you.

<%: Model.MyString %>

根据经验,除非有充分理由不这样做,否则应始终使用<%:标记 输出字符串.

As a rule of thumb you should always output your strings using the <%: tag, unless you have a good reason not to.

结帐

Checkout Scott Gu's blog for more information on the subject.

通过ASP.NET 4,我们引入了 新的代码表达式语法(<%:%>) 呈现类似<%=%>块的输出 可以-但也可以自动HTML 在这样做之前对其进行编码.这 消除了显式HTML的需要 编码内容...

With ASP.NET 4 we are introducing a new code expression syntax (<%: %>) that renders output like <%= %> blocks do – but which also automatically HTML encodes it before doing so. This eliminates the need to explicitly HTML encode content...

这篇关于MVC2&lt;%:标签与&lt;%=有何不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆