asp.net-mvc 用于新 ASP.NET MVC 3 站点的 Razor 与 Webforms 视图引擎
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5261124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Razor vs Webforms view engine for new ASP.NET MVC 3 site
提问by BritishDeveloper
Razor is prettier (and is new therefore cool).
Razor 更漂亮(而且是新的,因此很酷)。
Webforms is something I am already familiar with.
Webforms 是我已经熟悉的东西。
Naturally I would unquestionably go for the new thing to learn - Razor. But I have heard of two disadvantages that worry me:
毫无疑问,我会毫无疑问地去学习新的东西 - Razor。但我听说过两个让我担心的缺点:
- can't easily reuse existing web forms controls - in the rare instance I may need to drag something across... I'll reiterate - 'RARE'
- I hear it is less efficient at rendering (takes longer than web forms)
- 无法轻松重用现有的 Web 表单控件 - 在极少数情况下,我可能需要将某些内容拖过......我会重申 - 'RARE'
- 我听说它的渲染效率较低(比 Web 表单花费的时间更长)
So what are the advantages of Razor over Webforms view engines when using ASP.NET MVC 3?
那么在使用 ASP.NET MVC 3 时,Razor 相对于 Webforms 视图引擎有哪些优势?
采纳答案by marcind
This has already been addressed by the following question: Does Razor syntax provide a compelling advantage in UI markup?
以下问题已经解决了这个问题:Razor 语法是否在 UI 标记中提供了引人注目的优势?
回答by GvS
The Web form controls are already not very usable in Asp.Net Mvc. Since the MVC does not have the ViewState/Postback mechanism, and most controls need to use that. (Webforms: 0 Points, Razor 0 Points)
Web 表单控件在 Asp.Net Mvc 中已经不是很可用了。由于 MVC 没有 ViewState/Postback 机制,大多数控件都需要使用它。(网络表格:0 分,Razor 0 分)
Syntax in @Razor is much prettier. (Webforms: 0 Points, Razor 1 Points)
@Razor 中的语法更漂亮。(网络表格:0 分,Razor 1 分)
Don't know about the performance. I think that the (one time) parsing and rendering is such a small step of the whole pipeline, this should not matter. And might be changed by future updates. (Webforms: 0 Points, Razor 0 Points)
不知道性能怎么样。我认为(一次)解析和渲染是整个管道的一小步,这应该无关紧要。并且可能会被未来的更新所改变。(网络表格:0 分,Razor 0 分)
回答by BoxerBucks
You may want to clarify the technologies you are talking about. In ASP.Net WebForms is a web framework just like ASP.Net MVC is a web framework. The Razor view engine is a new view engine that is a different option to the default view engine in ASP.Net MVC web framework applications.
您可能想澄清您所谈论的技术。在 ASP.Net WebForms 是一个 Web 框架,就像 ASP.Net MVC 是一个 Web 框架一样。Razor 视图引擎是一种新的视图引擎,它与 ASP.Net MVC Web 框架应用程序中的默认视图引擎不同。
The main benefit to the Razor view engine is that it makes writing dynamic content on your MVC views easier because you don't have to use the <% %> formatting like you do with the default view engine. There are other benefits and Scott Gu has a good blog about them here.
Razor 视图引擎的主要好处是它使在 MVC 视图上编写动态内容变得更容易,因为您不必像使用默认视图引擎那样使用 <% %> 格式。还有其他好处,Scott Gu在这里有一篇关于它们的好博客。
回答by John W Hindes III
Learning curves can be a bitch, but hang in there. The code is not just "prettier", it's cleaner, and let's you work in a modular fashion, you can inject html sections that let you reuse code, much like object oriented code, but using just HTML.
学习曲线可能很糟糕,但要坚持下去。代码不仅“更漂亮”,而且更简洁,让您以模块化方式工作,您可以注入 html 部分,让您重用代码,就像面向对象的代码一样,但只使用 HTML。
This cleaner code means that 500 unique lines are now just 20! As for view state you can still uses hidden fields, and once you learn to code without, it wont be missed.
这个更简洁的代码意味着 500 行独特的行现在只有 20 行!至于视图状态,你仍然可以使用隐藏字段,一旦你学会了不用的编码,它就不会错过。
VS2013 now has an MVC Razor sample project with F# that will get you up to speed in no time.
VS2013 现在有一个带有 F# 的 MVC Razor 示例项目,可以让您立即上手。
The same project in Razor with F# that uses 500 lines of code would require 5000 or more lines of C# and Webform code.
使用 500 行代码的 Razor 和 F# 中的同一项目将需要 5000 行或更多行 C# 和 Webform 代码。
I would recommend you learn this new technology before a 16 year old intern takes your job ;-)
我建议你在 16 岁的实习生接受你的工作之前学习这项新技术;-)