Comparions of Razor vs ASPX syntax [closed]

2020-06-08 07:48发布

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:

enter image description here

enter image description here

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?

4条回答
甜甜的少女心
2楼-- · 2020-06-08 08:03

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.

查看更多
啃猪蹄的小仙女
3楼-- · 2020-06-08 08:11

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

查看更多
做个烂人
4楼-- · 2020-06-08 08:17

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.

查看更多
Lonely孤独者°
5楼-- · 2020-06-08 08:18

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.

查看更多
登录 后发表回答