新 ASP.NET MVC 3 站点的 Razor 与 Webforms 视图引擎
剃刀更漂亮(而且是新的,所以很酷)。
Webforms 是我已经熟悉的东西。
当然,我毫无疑问会去学习新东西——Razor。但我听说有两个让我担心的缺点:
- 无法轻松重用现有的 Web 表单控件 - 在极少数情况下,我可能需要拖动一些东西......我会重申 - “罕见”
- 我听说它的效率较低渲染(比 Web 表单花费更长的时间)
那么,在使用 ASP.NET MVC 3 时,Razor 相对于 Webforms 视图引擎有哪些优势呢?
Razor is prettier (and is new therefore cool).
Webforms is something I am already familiar with.
Naturally I would unquestionably go for the new thing to learn - Razor. But I have heard of two disadvantages that worry me:
- 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)
So what are the advantages of Razor over Webforms view engines when using ASP.NET MVC 3?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下问题已经解决了这个问题: Razor 是否可以使用语法在 UI 标记中提供了引人注目的优势?
This has already been addressed by the following question: Does Razor syntax provide a compelling advantage in UI markup?
Web 表单控件在 Asp.Net Mvc 中已经不太好用。由于MVC没有ViewState/Postback机制,而大多数控件都需要使用它。 (Webforms:0 分,Razor 0 分)
@Razor 中的语法更漂亮。 (Webforms:0 分,Razor 1 分)
不知道性能如何。我认为(一次)解析和渲染只是整个管道的一小步,这应该不重要。并且可能会因未来的更新而改变。 (网络表格:0 分,Razor 0 分)
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)
Syntax in @Razor is much prettier. (Webforms: 0 Points, Razor 1 Points)
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)
您可能想澄清您正在谈论的技术。在 ASP.Net WebForms 中,它是一个 Web 框架,就像 ASP.Net MVC 是一个 Web 框架一样。 Razor 视图引擎是一种新的视图引擎,与 ASP.Net MVC Web 框架应用程序中的默认视图引擎不同。
Razor 视图引擎的主要好处是它使在 MVC 视图上编写动态内容变得更容易,因为您不必使用 <% %>像使用默认视图引擎一样进行格式化。还有其他好处,Scott Gu 有一个关于它们的很好的博客 在这里。
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.
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.
学习曲线可能很糟糕,但坚持下去。代码不仅“更漂亮”,而且更干净,让您以模块化方式工作,您可以注入 html 部分,让您重用代码,就像面向对象的代码一样,但仅使用 HTML。
这个更简洁的代码意味着 500 个唯一行现在只剩下 20 个!至于视图状态,您仍然可以使用隐藏字段,一旦您学会了不使用隐藏字段进行编码,就不会错过它。
VS2013 现在有一个使用 F# 的 MVC Razor 示例项目,可以让您立即上手。
使用 F# 的 Razor 中使用 500 行代码的同一项目将需要 5000 行或更多行 C# 和 Webform 代码。
我建议您在 16 岁的实习生接手您的工作之前学习这项新技术;-)
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.
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.
VS2013 now has an MVC Razor sample project with F# that will get you up to speed in no time.
The same project in Razor with F# that uses 500 lines of code would require 5000 or more lines of C# and Webform code.
I would recommend you learn this new technology before a 16 year old intern takes your job ;-)