Can somebody explain me the difference between using <%: and <%= in an aspx view of an MVC application.
Thanks, Radu
Roughly speaking, <%=term%>
translates to Resonse.Write(term)
whereas <%:term%>
translates to Response.Write(Html.Encode(term))
<%:
does an HTML Encode, whereas <%=
does not.
It automatically html encodes the text that is rendered in the webpage.
<%: %>
will attempt to convert whatever is placed in between it to a string then Html Encodes it and outputs it to the response stream.. However, if the object implements IHtmlString
, it will NOT encode it.
<%= %>
will convert whatever is between it to a string and output it to the response stream.
The <%: was a new syntax that came with ASP.NET 4.0 and in effect it automatically HtmlEncodes the contents contained within the tags.
精彩评论