问题
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:
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.
回答2:
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.
回答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 %>
回答4:
It is difficult to be clear and definitive about names because Microsoft is often not clear and definitive. For example I do not know what the name of <%: ... %>
is.
It is also difficult to be certain of what the current documentation is and Microsoft is working on solving that problem.
The following table cross-references what is in Introduction to ASP.NET inline expressions in the .NET Framework with what is in ASP.NET Page Syntax.
Syntax | Support Article | Docs <% ... %> | Embedded Code Block | inline code (see Code Render Blocks) <%= ... %> | Inline Expression Block | inline expression (see Code Render Blocks) <%@ ... %> | Text Template Directive | Text Template Directive <%# ... %> | Data-Binding Expression | Data-Binding Expression <%$ ... %> | Expression Builder | ??? <%-- ... %> | Server-Side Comments | Server-Side Comments <%: ... %> | N/A | N/A
<% ... %>: Embedded Code Block
The Embedded Code Block provides backward compatibility with classical ASP and is also used by PHP and JSP. Since they are embedded within HTML they tend to make HTML difficult to read and maintain.
<%= ... %>: Inline Expression Block
An Inline Expression Block is executed as if it is a parameter of a Response.Write(…) statement.
<%: ... %>: ???
The same as <%= Server.HtmlEncode(...) %> where ... is the parameter of Server.HtmlEncode.
<%@ ... %>: Text Template Directive
The Text Template Directive specifies settings used by the page and by user control compilers when they process ASP.NET Web Form (.aspx) pages and User Control (.ascx) files.
<%# ... %>: Data-Binding Expression
The Data-Binding Expression binds a server control property to a data source.
<%$ ... %>: Expression Builder
The Expression Builder sets the value of a control's property to the value in an application's configuration or resource file. An Expression Builder expression consists of:
Expression Prefix: Expression Value
Where the Expression Prefix is the kind of expression such as a node in the Web.config file and Expression Value is the name of a key in the node. The result is the value specified for the key.
<%-- ... %>: Server-Side Comments Block
The Server-Side Comments Block allows comments to be placed anywhere in HTML except in code blocks.
Miscellaneous Syntax
The following are also in the Microsoft Docs documentation page, in case that helps.
Syntax | Docs <tagprefix:tagname runat="server"/> | Custom Server Control <object id="id" runat="server"/> | Server-Side Object Tag <!-- #include file|virtual="filename" --> | Server-Side Include Directive
回答5:
In Introducing Visual C# 2010, author Adam Freeman writes:
The official term for the <% and %> tags is server-side scripting delimiters although they are more commonly referred to as code nuggets.
来源:https://stackoverflow.com/questions/8205546/net-code-nugget-blocks