Asp。净Webforms和Asp。净网站(剃须刀)Vs Asp。Net MVC

时间:2022-06-01 05:41:34

I think Microsoft must have a reason for enhancing ASP.Net with RAZOR syntax.

我认为微软应该有一个增强ASP的理由。净与剃须刀语法。

On the Create New Website Project dialog of visual studio, there is another option for creating ASP.Net (Razor). The first time I came across the term Razor was when I read a book on Asp.Net MVC, I didn't know it exists for ASP.Net

在visual studio的新建网站项目对话框中,还有一个创建ASP的选项。净(剃须刀)。我第一次遇到Razor这个词是在我读一本关于Asp的书的时候。Net MVC,我不知道它存在于ASP.Net中

Asp。净Webforms和Asp。净网站(剃须刀)Vs Asp。Net MVC

I know what the Razor syntax is for, introduced in MVC 3. Before asking this question, I decided to create a test project for ASP.Net (Razor) and see how it is different from the normal ASP.Net webforms and ASP.Net MVC. I discovered no Model/View/Controllers folders like we have in MVC.

我知道Razor语法是用来做什么的,在MVC 3中已经介绍过了。在提出这个问题之前,我决定为ASP创建一个测试项目。看看它和普通的ASP有什么不同。净webforms和ASP。净MVC。我发现在MVC中没有模型/视图/控制器文件夹。

I discovered that the Master page is specified differently and the Master page is cleaner, no more <asp:ContentPlaceHolder />

我发现主页面的指定方式不同,主页面更干净,不再

But my questions are:

但我的问题是:

  • I notice it uses .cshtml, are there no more codebehinds?
  • 我注意到它使用。cshtml,是不是没有更多的代码?
  • similarities and differences between ASP.Net (Razor) and ASP.Net MVC
  • ASP之间的异同。净(剃须刀)和ASP。Net MVC
  • Can it be extended to function like ASP.Net MVC e.g. adding Controller?View/Model, Custom Routing I guess?
  • 它能像ASP那样扩展功能吗?Net MVC,比如添加控制器?视图/模型,自定义路由?
  • Why does this exist when we have ASP.Net MVC? Wouldn't this encourage reluctance to move to ASP.Net MVC?
  • 当我们有ASP时,为什么会有这种情况呢?净MVC吗?这难道不会鼓励人们不愿迁移到ASP吗?净MVC吗?
  • and why would you choose ASP.Net+Razor over ASP.Net MVC?
  • 为什么选择ASP呢?净+剃须刀/ ASP。净MVC吗?

UPDATE: ASP.NET Web Pages in particular was designed to make it easy for people who already know HTML to add server processing to their pages. It's a good choice for students, hobbyists, people in general who are new to programming. It can also be a good choice for developers who have experience with non-ASP.NET web technologies

更新:ASP。NET Web页面的设计目的是使那些已经了解HTML的人能够很容易地将服务器处理添加到他们的页面中。这对学生、业余爱好者和对编程不熟悉的人来说是一个很好的选择。对于有非asp经验的开发人员来说,这也是一个很好的选择。网网络技术

Update ASP.NET Web Pages ASP.NET Web Pages targets developers who want a simple web development story, along the lines of PHP. In the Web Pages model, you create HTML pages and then add server-based code to the page in order to dynamically control how that markup is rendered. Web Pages is specifically designed to be a lightweight framework, and it's the easiest entry point into ASP.NET for people who know HTML but might not have broad programming experience — for example, students or hobbyists. It's also a good way for web developers who know PHP or similar frameworks to start using ASP.NET.

更新ASP。净ASP的网页。NET Web页面针对的是开发人员,他们希望按照PHP的思路开发一个简单的Web开发故事。在Web页面模型中,创建HTML页面,然后向页面添加基于服务器的代码,以便动态控制如何呈现标记。Web页面是专门设计成轻量级框架的,它是进入ASP的最容易的入口点。NET为了解HTML但可能没有广泛编程经验的人提供的,例如学生或爱好者。这也是了解PHP或类似框架的web开发人员开始使用ASP.NET的好方法。

Asp.Net Web Pages

Asp。网网页

Please, I need your technical opinion. Thanks.

我需要你的专业意见。谢谢。

6 个解决方案

#1


16  

This is a great question. First, lets characterize Razor.

这是一个很好的问题。首先,让描述剃须刀。

Razor is an engine that parses server-side code an emits Html, just like ASP.NET Web Forms only with different and arguably more streamlined and terse syntax.

Razor是一个解析服务器端代码的引擎,就像ASP一样。NET Web表单只具有不同的、可以说更简洁的语法。

Razor v. Web Forms Sidebar: In ASP.NET Web Forms you have to identify when you wanted to start writing server code with '<%' and then when you were done writing server code you needed to identify that with '%>'. I love ASP.NET Web Forms, but that's clunky. With Razor you identify when you want to start writing server code with '@' and then the next time you start writing a server tag (starting with '<') it "figures out" that you're done with server code. It's a more concise way to write html intermingled with some server code.

网页形式的侧边栏:在ASP。当您想要开始使用'<%'编写服务器代码时,您必须识别NET Web表单;当您完成编写服务器代码时,您需要使用'%>'标识服务器代码。我爱ASP。网络表单,但那是笨拙的。使用Razor,你可以确定什么时候开始用“@”写服务器代码,然后下次再开始写服务器标签(以“<”开头),它就会“发现”你已经用服务器代码写完了。这是一种将html与一些服务器代码结合在一起的更简洁的方式。

ASP.NET Web Pages is a framework for creating simple Web Applications. ASP.NET MVC is a framework for creating web applications with either the Web Forms or Razor engine using the Model-View-Controller (MVC) pattern. ASP.NET Web Forms is a framework for creating web applications using the Web Forms render engine.

ASP。NET Web页面是创建简单Web应用程序的框架。ASP。NET MVC是使用web表单或Razor引擎创建web应用程序的框架,使用模型-视图-控制器(MVC)模式。ASP。NET Web Forms是一个使用Web表单渲染引擎创建Web应用程序的框架。

Ultimately the goal is to provide choice based on the sophistication of the application that is being built. Understanding each with assist you in making the correct choice for your application.

最终的目标是基于正在构建的应用程序的复杂性提供选择。理解每一个,帮助你为你的申请做出正确的选择。

Additional Links:

额外的链接:

#2


16  

In the MVC pattern, M is the Model, C is the Controller, and V is the View. So, quite naturally, in the ASP.NET MVC model, there is the concept of a View Engine. Razor is simply one of the View Engines provided. The other one provided out-of-the-box is the "old" WebForms one (you can also write your own View Engine by the way). So Razor does not have the notion of code-behind which stays within the WebForms view engine boundaries.

在MVC模式中,M是模型,C是控制器,V是视图。所以,很自然地,在ASP中。NET MVC模型,有视图引擎的概念。Razor是提供的视图引擎之一。另一个提供开箱即用的是“旧的”WebForms(顺便说一下,您也可以编写自己的视图引擎)。所以Razor没有代码隐藏的概念,它只存在于WebForms视图引擎的边界内。

So this kinda says it all. Razor handles the View part of MVC (If you choose to use it instead of the WebForms one). It has nothing to do with M or C.

这就说明了一切。Razor处理MVC的视图部分(如果你选择使用它而不是WebForms)。它与M或C无关。

Personally, I would definitely go for the Razor View Engine if you choose the ASP.NET MVC pattern, or use plain WebForms without MVC, as Razor has been designed to be less verbose, more simple to use than the Webforms one. It's also simply more recent so it tries to be ... simply better :-)

就我个人而言,如果您选择ASP,我肯定会选择Razor视图引擎。NET MVC模式,或者使用没有MVC的纯WebForms,因为Razor被设计成比WebForms更简洁、更容易使用。它也只是最近的,所以它尝试…只是更好的:-)

As a side note, the Razor Parser can also be used outside of ASP.NET MVC. It's implemented in an assembly that does not rely on MVC nor Web assemblies at all. See here for more on this: http://www.west-wind.com/weblog/posts/2010/Dec/27/Hosting-the-Razor-Engine-for-Templating-in-NonWeb-Applications

顺便说一句,Razor解析器也可以在ASP之外使用。净MVC。它是在一个不依赖于MVC或Web程序集的程序集中实现的。有关这方面的更多信息,请参见这里:http://www.west- wind.com/weblog/posts/2010/dec/27 / hosting - engine -for templating -in- non - web应用程序

#3


3  

There are not code-behinds by default, but you can easily make your razor file inherits from your custom class:

默认情况下并没有代码隐藏,但是您可以轻松地使您的razor文件继承自您的自定义类:

@inherits Index

and then

然后

public class Index : WebViewPage { }

(More information here: http://www.compiledthoughts.com/2011/01/aspnet-mvc3-creating-razor-view-engine.html)

(更多信息:http://www.compiledthoughts.com/2011/01/aspnet-mvc3-creating-razor-view-engine.html)

This is more like an old-fashioned way of doing web, more asp3-like. The difference with mvc is that mvc provides a huge framework that supports real world applications (using routing, controller and actions, and not just "code there in the markup").

这更像是一种老式的web方式,更像asp3。与mvc的不同之处在于mvc提供了一个巨大的框架,它支持真实的应用程序(使用路由、控制器和操作,而不仅仅是“标记中的代码”)。

I think it exists for making things that are really simple, but I don't actually know...

我认为它的存在是为了制造简单的东西,但我不知道……

Finally, I would always chose asp.net mvc with razor.

最后,我总是选择使用razor的asp.net mvc。

Hope it helps

希望它能帮助

#4


1  

Well, You have two options:

你有两个选择:

  1. Use WebForms: use out-of-the box, ready to use server-side controls(mixed markup and business codes) and use master pages and skins, but face some complexity involved of these conveniences! :)

    使用WebForms:开箱即用,准备好使用服务器端控件(混合标记和业务代码),使用主页面和皮肤,但是要面对这些便利带来的复杂性!:)

  2. Use MVC: or use separated design model which gives you a more organized codebase. you can first make a design prototype, or first making a business codes, then building the other aspect, quite easy. even you can give the designer more control on his work, giving him/her the ability to do everything he/she wants. -> this one is my preferred choice because it gives me more control on my code, makes my code more concise and clean.

    使用MVC:或者使用分离的设计模型,这将给您一个更有组织的代码库。您可以先做一个设计原型,或者先做一个业务代码,然后构建另一个方面,非常简单。甚至你可以让设计师对他的作品有更多的控制权,让他/她有能力做他/她想做的任何事情。->这是我的首选,因为它给了我更多的控制我的代码,使我的代码更简洁和干净。

If you selected the MVC pattern, then you are faced with another options... View Engines

如果您选择了MVC模式,那么您将面临另一个选项……视图引擎

  1. Old MVC View Engine: it is not bad, but is a bit verbose
  2. 旧的MVC视图引擎:它不错,但是有点冗长
  3. Razor Engine: it does the same thing the #1 does, why you ever don't want to use it?! it replaces nothing of the MVC pattern. it is also more concise and easy, you have both the power of MVC and the simplicity of the Razor Engine. Razor is a smarter in-markup syntax, which helps you stay focused on your business, rather than writing "<%"s ! by using razor syntax you are provided just a benefit, the full power of MVC is at your fingertips!
  4. 剃刀引擎:它做同样的事情#1做,为什么你从来不想使用它?它没有替换MVC模式。它也更简洁和容易,你有MVC的力量和剃刀引擎的简单。Razor是一种更聪明的标记语法,它可以帮助你专注于你的业务,而不是写“<%”!通过使用razor语法,您可以得到一个好处,MVC的全部功能就在您的指尖!

You can also write simple asp.net web pages just with razor(C# or VB) syntax. (just like php)

您也可以使用razor(c#或VB)语法编写简单的asp.net网页。(就像php)

  • My personal choice would be MVC+Razor, The best combination!
  • 我个人的选择是MVC+Razor,最好的组合!

#5


0  

  1. There are no "code-behinds" in MVC, period. What you have are controllers, which exist both for Razor and non-Razor.
  2. MVC中没有代码隐藏。你拥有的是控制器,无论是剃须刀还是非剃须刀都有。
  3. They're just two different types of markup. (See next answer)
  4. 它们只是两种不同类型的标记。(见下一个回答)
  5. It's not replacing MVC, it's just another option. The traditional markup is very verbose. Razor syntax is succinct.
  6. 它并没有取代MVC,它只是另一个选项。传统的标记非常冗长。剃须刀语法简洁。

#6


0  

To get out of all the confusion by webform, MVC, Razor, WebPages and lot more to come from microsoft, I prefer html-->javascript-->webapi.

为了摆脱webform、MVC、Razor、WebPages等来自微软的混乱,我更喜欢html——>javascript——>webapi。

#1


16  

This is a great question. First, lets characterize Razor.

这是一个很好的问题。首先,让描述剃须刀。

Razor is an engine that parses server-side code an emits Html, just like ASP.NET Web Forms only with different and arguably more streamlined and terse syntax.

Razor是一个解析服务器端代码的引擎,就像ASP一样。NET Web表单只具有不同的、可以说更简洁的语法。

Razor v. Web Forms Sidebar: In ASP.NET Web Forms you have to identify when you wanted to start writing server code with '<%' and then when you were done writing server code you needed to identify that with '%>'. I love ASP.NET Web Forms, but that's clunky. With Razor you identify when you want to start writing server code with '@' and then the next time you start writing a server tag (starting with '<') it "figures out" that you're done with server code. It's a more concise way to write html intermingled with some server code.

网页形式的侧边栏:在ASP。当您想要开始使用'<%'编写服务器代码时,您必须识别NET Web表单;当您完成编写服务器代码时,您需要使用'%>'标识服务器代码。我爱ASP。网络表单,但那是笨拙的。使用Razor,你可以确定什么时候开始用“@”写服务器代码,然后下次再开始写服务器标签(以“<”开头),它就会“发现”你已经用服务器代码写完了。这是一种将html与一些服务器代码结合在一起的更简洁的方式。

ASP.NET Web Pages is a framework for creating simple Web Applications. ASP.NET MVC is a framework for creating web applications with either the Web Forms or Razor engine using the Model-View-Controller (MVC) pattern. ASP.NET Web Forms is a framework for creating web applications using the Web Forms render engine.

ASP。NET Web页面是创建简单Web应用程序的框架。ASP。NET MVC是使用web表单或Razor引擎创建web应用程序的框架,使用模型-视图-控制器(MVC)模式。ASP。NET Web Forms是一个使用Web表单渲染引擎创建Web应用程序的框架。

Ultimately the goal is to provide choice based on the sophistication of the application that is being built. Understanding each with assist you in making the correct choice for your application.

最终的目标是基于正在构建的应用程序的复杂性提供选择。理解每一个,帮助你为你的申请做出正确的选择。

Additional Links:

额外的链接:

#2


16  

In the MVC pattern, M is the Model, C is the Controller, and V is the View. So, quite naturally, in the ASP.NET MVC model, there is the concept of a View Engine. Razor is simply one of the View Engines provided. The other one provided out-of-the-box is the "old" WebForms one (you can also write your own View Engine by the way). So Razor does not have the notion of code-behind which stays within the WebForms view engine boundaries.

在MVC模式中,M是模型,C是控制器,V是视图。所以,很自然地,在ASP中。NET MVC模型,有视图引擎的概念。Razor是提供的视图引擎之一。另一个提供开箱即用的是“旧的”WebForms(顺便说一下,您也可以编写自己的视图引擎)。所以Razor没有代码隐藏的概念,它只存在于WebForms视图引擎的边界内。

So this kinda says it all. Razor handles the View part of MVC (If you choose to use it instead of the WebForms one). It has nothing to do with M or C.

这就说明了一切。Razor处理MVC的视图部分(如果你选择使用它而不是WebForms)。它与M或C无关。

Personally, I would definitely go for the Razor View Engine if you choose the ASP.NET MVC pattern, or use plain WebForms without MVC, as Razor has been designed to be less verbose, more simple to use than the Webforms one. It's also simply more recent so it tries to be ... simply better :-)

就我个人而言,如果您选择ASP,我肯定会选择Razor视图引擎。NET MVC模式,或者使用没有MVC的纯WebForms,因为Razor被设计成比WebForms更简洁、更容易使用。它也只是最近的,所以它尝试…只是更好的:-)

As a side note, the Razor Parser can also be used outside of ASP.NET MVC. It's implemented in an assembly that does not rely on MVC nor Web assemblies at all. See here for more on this: http://www.west-wind.com/weblog/posts/2010/Dec/27/Hosting-the-Razor-Engine-for-Templating-in-NonWeb-Applications

顺便说一句,Razor解析器也可以在ASP之外使用。净MVC。它是在一个不依赖于MVC或Web程序集的程序集中实现的。有关这方面的更多信息,请参见这里:http://www.west- wind.com/weblog/posts/2010/dec/27 / hosting - engine -for templating -in- non - web应用程序

#3


3  

There are not code-behinds by default, but you can easily make your razor file inherits from your custom class:

默认情况下并没有代码隐藏,但是您可以轻松地使您的razor文件继承自您的自定义类:

@inherits Index

and then

然后

public class Index : WebViewPage { }

(More information here: http://www.compiledthoughts.com/2011/01/aspnet-mvc3-creating-razor-view-engine.html)

(更多信息:http://www.compiledthoughts.com/2011/01/aspnet-mvc3-creating-razor-view-engine.html)

This is more like an old-fashioned way of doing web, more asp3-like. The difference with mvc is that mvc provides a huge framework that supports real world applications (using routing, controller and actions, and not just "code there in the markup").

这更像是一种老式的web方式,更像asp3。与mvc的不同之处在于mvc提供了一个巨大的框架,它支持真实的应用程序(使用路由、控制器和操作,而不仅仅是“标记中的代码”)。

I think it exists for making things that are really simple, but I don't actually know...

我认为它的存在是为了制造简单的东西,但我不知道……

Finally, I would always chose asp.net mvc with razor.

最后,我总是选择使用razor的asp.net mvc。

Hope it helps

希望它能帮助

#4


1  

Well, You have two options:

你有两个选择:

  1. Use WebForms: use out-of-the box, ready to use server-side controls(mixed markup and business codes) and use master pages and skins, but face some complexity involved of these conveniences! :)

    使用WebForms:开箱即用,准备好使用服务器端控件(混合标记和业务代码),使用主页面和皮肤,但是要面对这些便利带来的复杂性!:)

  2. Use MVC: or use separated design model which gives you a more organized codebase. you can first make a design prototype, or first making a business codes, then building the other aspect, quite easy. even you can give the designer more control on his work, giving him/her the ability to do everything he/she wants. -> this one is my preferred choice because it gives me more control on my code, makes my code more concise and clean.

    使用MVC:或者使用分离的设计模型,这将给您一个更有组织的代码库。您可以先做一个设计原型,或者先做一个业务代码,然后构建另一个方面,非常简单。甚至你可以让设计师对他的作品有更多的控制权,让他/她有能力做他/她想做的任何事情。->这是我的首选,因为它给了我更多的控制我的代码,使我的代码更简洁和干净。

If you selected the MVC pattern, then you are faced with another options... View Engines

如果您选择了MVC模式,那么您将面临另一个选项……视图引擎

  1. Old MVC View Engine: it is not bad, but is a bit verbose
  2. 旧的MVC视图引擎:它不错,但是有点冗长
  3. Razor Engine: it does the same thing the #1 does, why you ever don't want to use it?! it replaces nothing of the MVC pattern. it is also more concise and easy, you have both the power of MVC and the simplicity of the Razor Engine. Razor is a smarter in-markup syntax, which helps you stay focused on your business, rather than writing "<%"s ! by using razor syntax you are provided just a benefit, the full power of MVC is at your fingertips!
  4. 剃刀引擎:它做同样的事情#1做,为什么你从来不想使用它?它没有替换MVC模式。它也更简洁和容易,你有MVC的力量和剃刀引擎的简单。Razor是一种更聪明的标记语法,它可以帮助你专注于你的业务,而不是写“<%”!通过使用razor语法,您可以得到一个好处,MVC的全部功能就在您的指尖!

You can also write simple asp.net web pages just with razor(C# or VB) syntax. (just like php)

您也可以使用razor(c#或VB)语法编写简单的asp.net网页。(就像php)

  • My personal choice would be MVC+Razor, The best combination!
  • 我个人的选择是MVC+Razor,最好的组合!

#5


0  

  1. There are no "code-behinds" in MVC, period. What you have are controllers, which exist both for Razor and non-Razor.
  2. MVC中没有代码隐藏。你拥有的是控制器,无论是剃须刀还是非剃须刀都有。
  3. They're just two different types of markup. (See next answer)
  4. 它们只是两种不同类型的标记。(见下一个回答)
  5. It's not replacing MVC, it's just another option. The traditional markup is very verbose. Razor syntax is succinct.
  6. 它并没有取代MVC,它只是另一个选项。传统的标记非常冗长。剃须刀语法简洁。

#6


0  

To get out of all the confusion by webform, MVC, Razor, WebPages and lot more to come from microsoft, I prefer html-->javascript-->webapi.

为了摆脱webform、MVC、Razor、WebPages等来自微软的混乱,我更喜欢html——>javascript——>webapi。