Razor和ASPX语法的比较

时间:2022-07-12 22:35:12

Recently I've been getting into MVC3 and Razor, and reading several articles about these topics to better understand the concept.

最近,我开始学习MVC3和Razor,并阅读了一些关于这些主题的文章,以更好地理解这个概念。

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:

我一直在阅读Scott Guthrie的博客文章,他提供了以下两个例子。第一个是ASPX语法,第二个是Razor语法:

Razor和ASPX语法的比较

Razor和ASPX语法的比较

This is great, but as a fair comparison of ASPX and Razor I'm a bit confused.

这很棒,但是作为ASPX和Razor的比较,我有点困惑。

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.

我从未见过这样的ASPX语法示例。开发人员肯定使用了中继器控件等。网络被释放?自从经典的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?

做任何ASP。NET ASPX开发人员的代码是这样的吗?

4 个解决方案

#1


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).

我想你一定很困惑。你的第一个例子不是传统的asp.net。它是MVC渲染引擎用来使用pre-razor (MVC 1和MVC 2——我认为它被称为WebForms视图引擎)。

I am very sure you cannot use a foreach in traditional ASP.Net pages. (Veli's comment corrected me on this)

我很肯定在传统ASP中不能使用foreach。网络页面。(Veli的评论纠正了我的看法)

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.

使用MVC 3,您可以选择使用Razor视图引擎,正如您在示例中看到的那样,它的可读性要高得多。当然,使用MVC 3,您仍然可以选择用另一种方式来完成,这是您在创建初始项目时选择的选项。

You can read more about the differences here

您可以在这里阅读更多关于差异的信息

#2


6  

Do any ASP.NET ASPX developers code like this?

做任何ASP。NET ASPX开发人员的代码是这样的吗?

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

我对此表示怀疑。典型的WebForms开发人员使用服务器端控件,比如中继器。

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.

ASP。另一方面,NET MVC开发人员非常使用这种语法。实际上,你可以在ASP中使用显示模板。NET MVC,避免了编写难看的foreach循环。

#3


0  

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.

这对于使用MVVM体系结构的c# WinForm/WPF开发人员来说是正确的。但是,我认为将编程代码内联放到ASP中仍然是一个很好的实践。净MVC。

#4


-1  

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.

MVC应该被视为一个普通的MVC框架,所有的逻辑代码都应该在控制器中。

#1


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).

我想你一定很困惑。你的第一个例子不是传统的asp.net。它是MVC渲染引擎用来使用pre-razor (MVC 1和MVC 2——我认为它被称为WebForms视图引擎)。

I am very sure you cannot use a foreach in traditional ASP.Net pages. (Veli's comment corrected me on this)

我很肯定在传统ASP中不能使用foreach。网络页面。(Veli的评论纠正了我的看法)

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.

使用MVC 3,您可以选择使用Razor视图引擎,正如您在示例中看到的那样,它的可读性要高得多。当然,使用MVC 3,您仍然可以选择用另一种方式来完成,这是您在创建初始项目时选择的选项。

You can read more about the differences here

您可以在这里阅读更多关于差异的信息

#2


6  

Do any ASP.NET ASPX developers code like this?

做任何ASP。NET ASPX开发人员的代码是这样的吗?

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

我对此表示怀疑。典型的WebForms开发人员使用服务器端控件,比如中继器。

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.

ASP。另一方面,NET MVC开发人员非常使用这种语法。实际上,你可以在ASP中使用显示模板。NET MVC,避免了编写难看的foreach循环。

#3


0  

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.

这对于使用MVVM体系结构的c# WinForm/WPF开发人员来说是正确的。但是,我认为将编程代码内联放到ASP中仍然是一个很好的实践。净MVC。

#4


-1  

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.

MVC应该被视为一个普通的MVC框架,所有的逻辑代码都应该在控制器中。