What is the difference between and ?

What is the difference between <% %> and <%= %> in ASP.NET MVC? And when to use which?


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

See also this question. MVC hasn’t changed how these bee-stings work, just how often they’re used.


  • <% – execute code
  • <%@ – page directive
  • <%$ – resource access
  • <%= – explicit output to page
  • <%# – data binding
  • <%-- – server side comment block

Also new in ASP.Net 4:

  • <%: – writes out to the page, but with HTML encoded

Method 2

<%= %> writes to the output stream (usually html) while <% %> is for executing arbitrary script code.

Method 3

Say you have a method on your page, called “SayHello”:

protected string SayHello()
    return "Hello!";

And on your page, you have these statements:

first: <%= SayHello() %>
second: <% SayHello() %>

Your output will be:

first: Hello!

when you use <%= %>, what you put in there is inserted into the html at that position. If you use <% %>, you’re just inserting some code into your page.

Method 4

<%= echos the statement out.

<% just runs it.

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x