Comparions of Razor vs ASPX syntax [closed]

2020-06-08 07:27发布

问题:

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?

回答1:

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



回答2:

Do any ASP.NET ASPX developers code like this?

I doubt it. Classic WebForms developers use server side controls such as repeaters.

ASP.NET MVC developers on the other hand use this syntax very much. Well, actually you could use Display Templates in ASP.NET MVC which avoids you writing ugly foreach loops.



回答3:

I always thought it was bad practice to put programming code inline with the markup file, instead of the code behind file.

That's true for C# WinForm/WPF developer that use MVVM architecture. But, I think it is still good practice to put programming code inline in ASP.NET MVC.



回答4:

Do not confuse programming code and code to display a property. It is bad practice to put programming code in a markup file. However the code you are showing is code used to make the markup.

MVC should be treated as a normal MVC framework and all logic code should be in the Controller.