Fabiano
Fabiano

Reputation: 5194

What does mean ":" in <%: and what is the difference to <%=?

In ASP.NET MVC 2 <%: tag was introduced to replace <%= for Html helpers. But what does it mean and what is the difference to the previous one? When shall I use <%= and when <%:?

Thank you

Upvotes: 8

Views: 207

Answers (3)

Joe Phillips
Joe Phillips

Reputation: 51100

In ASP.NET 4 the <%: xyz %> syntax will do the same thing as <%= Server.HtmlEncode(xyz) %> did in previous versions. It is simply a shortcut because it is used so often.

As Richard says below, it can also determine if a string does not need to be encoded based on whether or not it implements the IHtmlString interface.

Upvotes: 13

MojoFilter
MojoFilter

Reputation: 12276

<%= Injects the value directly whereas <%: automatically escapes all of the scary special characters for you.

In other words,

<%: myString %>

is the same as

<%= Server.HtmlEncode(myString) %>

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1499860

IIRC, <%: automatically provides HTML encoding so you don't need to do it yourself.

From Scott Guthrie's blog post:

With ASP.NET 4 we are introducing a new code expression syntax (<%: %>) that renders output like <%= %> blocks do – but which also automatically HTML encodes it before doing so.

Read the blog post for a lot more detail.

Upvotes: 8

Related Questions