views:

378

answers:

3

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.

+10  A: 

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.

Rob Fonseca-Ensor
+1 for the great read you linked.
Matt Ball
thanks, @bears. phil's blog is a must read - especially if you're working with ASP.NET MVC
Rob Fonseca-Ensor
I love the reference to the colon being equals but viewed from the side :-)
Jamie Dixon
+1  A: 

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.

Matthew Manela
The key word you're missing here is "HTML," as in "HTML encoded" - not just "encoded."
Matt Ball
+1  A: 

<%= 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

gahooa