Nathan R Nathan R - 1 year ago 117
ASP.NET (C#) Question

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:
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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download