In ASP.NET MVC, what is the difference between <% = and <% :?

I am new to ASP.NET MVC. I saw both <% = ...%> and <%: ...%> . I am familiar with the first of the classic ASP days, but not with the last. What is the difference between the two?

+4
source share
3 answers

Usage <%: tells ASP.NET 4.0 to execute Server.HtmlEncode () for the displayed value.
Whereas using <% =, the developer should use Server.HtmlEncode ().
Note. HtmlEncode () helps to avoid cross-site scripting attacks.

For more information, see the ScottGu article here .

+4
source

<%= %> is the equivalent of response.write in classic ASP.

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

<%: %> is a new shortcut for .NET 4 that represents <%= html.encode(item) %>

Link to the video explaining the shortcut (this is a short clip):

+11
source

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

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

Go here for more details.

+1
source

Source: https://habr.com/ru/post/1313454/


All Articles