Syntax ASP.NET/MVC

Possible duplicate:
ASP.NET Special Tags

I am trying to understand an MVC application. It has several user controls. In these controls, I see syntax like:
<%= ...some text ... %>

I also saw:
<%: ...some text ... %>
<@ ...some text ... %>
<% ...some text ... %>
<%# ...some text ... %>

I see that it allows me to write code in the / javascript control, but I don't quite understand the difference between%,%:,% = and% #.

When are they performed / evaluated?

Is there any difference if <%= ... => is in a user control or enclosed in single quotes in a Javascript function?

I’m not even sure if my title is correct, I don’t get anything when I come up with it. Therefore, I would be happy to explain, more than happy with reference to the documentation and pleased with the correct terminology.

+6
source share
3 answers

Assuming you are using the WebForms view engine in ASP.NET MVC, all you need to know is the following:

  • <%= %> - Print the result of the input evaluation in response. For example, <%= "<div>foo</div>" %> displays <div>foo</div> .
  • <%: %> is the same as the first, except that HTML encodes the output. Thus, <%: "<div>foo</div>" %> will output &lt;div&gt;foo&lt;/div&gt; .
  • <% %> - evaluates the expression on the server, but displays nothing in response. For example, you can declare a variable: <% string foo = "foo bar"; %> <% string foo = "foo bar"; %> , which can later be used to output with one of the two previous methods.
  • <%@ %> - This is only used to define page directives or control presentation. It also allows you to enter namespaces and assemblies in scope for a given view. For example, <%@ Import Namespace="System.IO" %> will bring the System.IO namespace into scope.

As for <%# , this is not used in ASP.NET MVC. This concept is called data binding and only works in classic WebForms applications.

+4
source

<%= ...some text ... %> : contains an expression. The result of this expression will be a call to .ToString() , and the resulting string will be output directly.

<%: ...some text ... %> : contains an expression. If the result of the expression is IHtmlString , .ToHtmlString() will be called on it, and the results will be output directly. Otherwise, the result of this expression will have .ToString() called on it, and the resulting string will be escaped before exiting.

<@ ...some text ... %> : contains the page directive for objects such as namespaces that should be included. This is a compiler directive and is evaluated when compiling a view (usually the first time a view is called).

<% ...some text ... %> : contains one or more operators. These instructions are followed.

<%# ...some text ... %> : This is a data binding expression in ASP.NET WebForms. I do not think this is the correct syntax for MVC View. However, you continue to mention “user controls” that are not really part of the MVC paradigm, so you may need to reconsider whether this question really relates to MVC.

+1
source

<%# ...some text ... %> - for expressions that bind data; for example, when you have something like:

 <asp:itemtemplate> <asp:label Text='<%#Eval("Property")%>' .../> </asp:itemtemplate> 

<%= some text %> is basically a shortcut for Response.Write()

<%@ some text %> are for application directives; for example, when you need to import a namespace: <%@ Import namespace="value" %>

I just noticed a Stripling warrior answer that complements this ...

0
source

All Articles