In ASP.NET MVC 2 <%:
tag was introduced to replace <%=
for Html helpers. But what does it mean and what is the difference to the previous one? When shall I use <%=
and when <%:
?
在ASP.NET MVC 2中引入了<%:标记来替换<%=用于Html帮助程序。但这是什么意思,与前一个有什么不同?我应该何时使用<%=和何时<%:?
Thank you
谢谢
3 个解决方案
#1
14
In ASP.NET 4 the <%: xyz %>
syntax will do the same thing as <%= Server.HtmlEncode(xyz) %>
did in previous versions. It is simply a shortcut because it is used so often.
在ASP.NET 4中,<%:xyz%>语法将与先前版本中的<%= Server.HtmlEncode(xyz)%>执行相同的操作。它只是一个捷径,因为它经常被使用。
As Richard says below, it can also determine if a string does not need to be encoded based on whether or not it implements the IHtmlString
interface.
正如Richard在下面所说,它还可以确定是否需要根据字符串是否实现IHtmlString接口来编码字符串。
#2
8
IIRC, <%:
automatically provides HTML encoding so you don't need to do it yourself.
IIRC,<%:自动提供HTML编码,因此您无需自己动手。
From Scott Guthrie's blog post:
来自Scott Guthrie的博文:
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.在ASP.NET 4中,我们引入了一种新的代码表达式语法(<%:%>),它可以像<%=%>块一样呈现输出 - 但在执行此操作之前,它还会自动对HTML进行编码。
Read the blog post for a lot more detail.
阅读博客文章了解更多详情。
#3
3
<%= Injects the value directly whereas <%: automatically escapes all of the scary special characters for you.
<%=直接注入值,而<%:自动为您转义所有可怕的特殊字符。
In other words,
换一种说法,
<%: myString %>
<%:myString%>
is the same as
是相同的
<%= Server.HtmlEncode(myString) %>
<%= Server.HtmlEncode(myString)%>
#1
14
In ASP.NET 4 the <%: xyz %>
syntax will do the same thing as <%= Server.HtmlEncode(xyz) %>
did in previous versions. It is simply a shortcut because it is used so often.
在ASP.NET 4中,<%:xyz%>语法将与先前版本中的<%= Server.HtmlEncode(xyz)%>执行相同的操作。它只是一个捷径,因为它经常被使用。
As Richard says below, it can also determine if a string does not need to be encoded based on whether or not it implements the IHtmlString
interface.
正如Richard在下面所说,它还可以确定是否需要根据字符串是否实现IHtmlString接口来编码字符串。
#2
8
IIRC, <%:
automatically provides HTML encoding so you don't need to do it yourself.
IIRC,<%:自动提供HTML编码,因此您无需自己动手。
From Scott Guthrie's blog post:
来自Scott Guthrie的博文:
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.在ASP.NET 4中,我们引入了一种新的代码表达式语法(<%:%>),它可以像<%=%>块一样呈现输出 - 但在执行此操作之前,它还会自动对HTML进行编码。
Read the blog post for a lot more detail.
阅读博客文章了解更多详情。
#3
3
<%= Injects the value directly whereas <%: automatically escapes all of the scary special characters for you.
<%=直接注入值,而<%:自动为您转义所有可怕的特殊字符。
In other words,
换一种说法,
<%: myString %>
<%:myString%>
is the same as
是相同的
<%= Server.HtmlEncode(myString) %>
<%= Server.HtmlEncode(myString)%>