Ybbest
Ybbest

Reputation: 1550

Why I need to Stop Using <%= … %> For Rendering and Start Using <%: … %> in Asp.net?

I read somewhere that I should stop using <%= … %> to render and start using <%: … %>.

Can anyone explain what are differences between <%= … %> and <%: … %>, and what are advantages of using one or another?

Here is the slidedeck I am reading

http://ssmith-presentations.s3.amazonaws.com/ASPNET_TipsTricksTools_April2010.zip

Here are the links you can get more information from

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

http://haacked.com/archive/2009/11/03/html-encoding-nuggets-aspnetmvc2.aspx

Upvotes: 13

Views: 191

Answers (2)

R. Martinho Fernandes
R. Martinho Fernandes

Reputation: 234554

Basically, <%: will HTML encode the result, while <%= won't. This helps prevent XSS attacks. You can read more about it in this series of blog posts by Phil Haack.

Upvotes: 4

Richard
Richard

Reputation: 22026

Actually it is a short version of <%=Server.HtmlEncode(string) %>

See this link

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

It is better practice in order to avoid Javascript attacks etc. So if someone adds a comment to your blog for example which has say an iframe html or javascript in it then it will be rendered exactly as typed and not with the JS or iframe actually working.

Upvotes: 11

Related Questions