Take the 2-minute tour ×

I'm new to ASP.NET MVC. I've seen both <%= ... %> and <%: ... %>. I'm familiar with the first from classic ASP days, but not the latter. What is the difference between the two?

share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

Using <%: tells ASP.NET 4.0 to perform a Server.HtmlEncode() on the value being displayed.
Whereas using <%=, it is up to the developer to use Server.HtmlEncode().
Note HtmlEncode() helps void cross-scripting attacks.

For more info, see ScottGu's post here.

share|improve this answer
add comment

<%= %> - equivalent to response.write in classic ASP.

<% %> - represents a code block, if, then else, for each, etc.

<%: %> - this is a shortcut new to .NET 4, this represents <%= html.encode(item) %>

Link to video explaining the shortcut (it's a short clip):

share|improve this answer
add comment

<%: expression %> is an HTML encoded expression and was introduced in ASP.NET 4

It is equivalent to <%= HttpUtility.HtmlEncode(expression) %>

Go here for more detail.

share|improve this answer
add comment

Your Answer


