Recently I've been getting into MVC3 and Razor, and reading several articles about these topics to better understand the concept.
I've been reading Scott Guthrie's Blog Post, in which he provides the following 2 examples. The first being ASPX syntax, the second being Razor syntax:
This is great, but as a fair comparison of ASPX and Razor I'm a bit confused.
I've never seen any working example of ASPX syntax like that. Surely developers have used Repeater
controls etc since ASP.NET was released? I haven't seen syntax like that since Classic ASP.
I always thought it was bad practice to put programming code inline with the markup file, instead of the code behind file.
Do any ASP.NET ASPX developers code like this?
Web Form Engine has the same syntax like Asp.net Web Forms uses for . aspx pages. By default, Razor Engine prevents XSS attacks(Cross-Site Scripting Attacks) means it encodes the script or html tags like <,> before rendering to view. Razor Engine is little bit slow as compared to Webform Engine.
The Razor View Engine is a bit slower than the ASPX View Engine. Razor provides a new view engine with streamlined code for focused templating. Razor's syntax is very compact and improves readability of the markup and code. By default MVC supports ASPX (web forms) and Razor View Engine.
It is light weight and it has a simple syntax. It is easier to understand Razor syntax as compared to ASPX view engine syntax. Provides automatic encoding of HTML output. Razor syntax code is more succinct.
One major advantage to aspx compared to cshtml is that you can view and edit the page itself (WUSIWYG kind of) using the design tab. With cshtml files you might as well use notepad to edit your html page. You are working "in the dark".
I think you are definitely getting confused. Your first example is not traditional asp.net. It is what the MVC render engine used to used pre-razor (MVC 1 & MVC 2 - which I believe is called the WebForms view engine).
I am very sure you cannot use a foreach in traditional ASP.Net pages. (Veli's comment corrected me on this)
With MVC 3 you can choose to use the Razor view engine which as you can see in the example is much more readable. Of course, with MVC 3 you can still choose to do it the other way, this is an option you select when creating the initial project.
You can read more about the differences here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With