.NET “code nugget blocks”?

2019-01-11 13:25发布

问题:

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.

回答1:

They are called code nuggets. 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.



回答2:

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.



回答3:

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 %>