So in .NET we have...
<%=
<%: (new to .NET 4 - syntactic sugar for HTML encoding)
<%#
<%@
What exactly are these?
And are there more? Is there an exhaustive list of these and what they are anywhere? It's hard to know what to even search for - I didn't even hear of the term "code nugget blocks" 'til tonight when I discovered what <%: was.
They are often called code nuggets but that term does not exist in the Microsoft documentation. Microsoft calls them inline expressions as in Introduction to ASP.NET inline expressions in the .NET Framework. They provide ASP.NET framework instruction on how to process the statement within those symbols (<% %>). Until I knew its name, yep it was a little harder to ask about it in the community. Not sure of an 'exhaustive' list, but there are a couple more than you have specified though. Below are the list of other code nuggets and their uses and sample example.
Symbol -- Name -- Description -- eg (Format)
<%
--Standard code nugget--Indicates that the following statements are C# statements. Will have to follow C# syntax rules. eg.
<% string[] cities = { ""London"", ""New York"", ""Paris"" };
string myCity = cities[new Random().Next(cities.Length)];
Response.Write(myCity);%>
<%=
--Content code nugget--Similar to standard cn, difference being the returned result is directly inserted into response to the browser without having to use Response.Write
. eg.
<%=textBox.Text%>
(NOT RECOMMENDED, includes risk of html injection attack. If the input on the text box is something like "< button type = submit > Submit</button >
", it will add a button to page. Of course there would be validation, but hope the point is clear.)
<%:
--Encoded code nugget --Similar to <%=, but the response is HTML encoded. eg.
Name is <%:textBox.Text%>
(whatever the input is on the text box, it is displayed. If the input is something like "< button type = submit > Submit</button >
", output would be "Name is <button type = submit> Submit</button>
".
<%#
--Data-binding code nugget --Denotes a data-binding code nugget, used to refer to the current data object. Only usable with databind controls like repeater etc.
<%#:
--Encoded data binding--Denotes an encoded data binding code nugget where the data-bound value is encoded. eg.
<asp:Repeater ItemType = ""System.String"" SelectMethod = ""GetCities"" runat = ""server">
<ItemTemplate>
<li > <%# Item % > </li>
</ItemTemplate>
</asp:Repeater>
(If encoded (<%#:
) is used, it displays literals without interpretations, recommended.)"
<%$
--Property code nugget--Used to refer to configuration value, such as those defined in Web.config.
<asp:Literal Text = " < %$ AppSettings: cityMessage % > " runat = "server" />
(Retrieves the value of cityMessage key from the config file.)
<%@
--Page directive--This is used to configure the Web Form (or control or master page, depending on the kind of directive. eg.
<%@ Page.. <%@ Master
All the above mentioned information and examples are from Adam Freeman's Pro ASP .NET 4.5 book, Chapter 12. Excellent book imo.