Nathan R
Nathan R

Reputation: 870

ASP.NET - Difference between @ syntax and <% %>?

I just started a job that uses ASP.NET, which I'm not terribly familiar with yet.

In most of the .cshtml files there are C# code nuggets with an @ sign, but then I also see <% %> lines that appear to be doing the same thing (though often the <%'s are more mixed in with HTML code).

The only thing I've found online that compares the two is this website: http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx/ which is helpful, but seems to show the two forms of syntax being used for the exact same things.

What is the difference? Why use one and not the other?

Upvotes: 4

Views: 220

Answers (1)

DavidG
DavidG

Reputation: 119186

WebForms is a very old technology (almost 15 years as I write this!) uses the <% ... %> syntax.

Early versions of MVC also used the same syntax as WebForms. Later on, MVC3 was released (around 2010) with the new syntax language called Razor, that used both the old style and the new @... syntax.

It's a little odd to see projects using both styles, especially in the same file, but my advice would be to stick with Razor syntax for anything new, and perhaps even look to refactoring out the older code for consistency.

Upvotes: 3

Related Questions