Are <%: and <%= the same thing as embbed code (expression) blocks

 ̄綄美尐妖づ 提交于 2019-11-26 11:19:50

问题


Having just started with MVC 2 I notice that in their starter template they use

<%: Html.ActionLink(\"Home\", \"Index\", \"Home\")%>

and I was sure that in MVC 1 it was

<%= Html.ActionLink(\"Home\", \"Index\", \"Home\")%>

Are they the same thing? If so, why the change from equal sign to colon.


回答1:


the colon syntax means you'll be html encoded automatically: http://haacked.com/archive/2009/09/25/html-encoding-code-nuggets.aspx

They couldn't just html encode all the existing <%= blocks, because things that are already properly encoded (which is hopefully most of the projects out there) would look strange.




回答2:


<%= is used for writing to the output buffer.

<%: is used for writing to the output buffer, after HTML Encoding the content... Unless the IHtmlString Interface has been implemented on the returned object.

Scott Guthrie has an excellent post on this topic: http://weblogs.asp.net/scottgu/archive/2010/04/06/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2.aspx

If the output has already been escaped, double encoding can be prevented by implementing the IHtmlString Interface on the returned object. http://msdn.microsoft.com/en-us/library/system.web.ihtmlstring.aspx




回答3:


ASP .NET 4 introduced the <%: syntax which encoded the output before rendering it to the screen. ASP MVC already was encoding this but to be more explicit they began using the syntax as well to make it clear that whenever you see the <%: you can be sure the output will be properly encoded.



来源:https://stackoverflow.com/questions/2676236/are-and-the-same-thing-as-embbed-code-expression-blocks

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!