Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET "code nugget blocks"?

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.

like image 684
geoid Avatar asked Nov 20 '11 23:11

geoid


3 Answers

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.

like image 98
anish Avatar answered Nov 05 '22 22:11

anish


These <%@ are directives. For an exhaustive list and documentation see MSDN.

When used, directives can be located anywhere in an .aspx or .ascx file, though standard practice is to include them at the beginning of the file. Each directive can contain one or more attributes (paired with values) that are specific to that directive.

The <% are script blocks. Here is a good description in MSDN

An embedded code block is server code that executes during the page's render phase. The code in the block can execute programming statements and call functions in the current page class.

The <%# block is used normally in bound controls. See this short article for more info.

like image 31
Ray K Avatar answered Nov 05 '22 23:11

Ray K


I'm not sure if all these WebForm tags have a proper collective name, but they should all be covered in ASP.NET Page Syntax.

There's another that's not on the list, ASP.NET Expressions:

<%$ expressionPrefix: expressionValue %>
like image 45
Chris Fulstow Avatar answered Nov 05 '22 21:11

Chris Fulstow