Tuesday, August 24, 2010

What is the difference in <%: variable%> and <%= variable%> in ASP.NET MVC?

With many folks adopting ASP.NET MVC as well as Visual Studio 2010, there is a fairly confusing change if you are one of those people who doesn’t engage in the alpha and beta phases of new product releases from Microsoft or any other vendor, for that matter.
The issue of <%: vs. <%= has had a great deal of discussion around it in the years leading up to the Visual Studio 2010 release.  This new expression syntax, or code nugget, is a new feature of ASP.NET 4.  It applies equally to ASP.NET Web Forms and ASP.NET MVC applications that are using the Web Forms view engine (ASPX).
In short, <%: ViewData["Message"] %> does the same thing as <%= Html.Encode(ViewData["Message"]) %>.
When creating a new ASP.NET MVC web application in Visual Studio 2010, you will see that the default Home/Index.aspx view is as follows.
Read more: Jeffrey Palermo (.com)