通过 ASP.NET MVC Razor 视图使用 Razor 的 ASP.NET 网页
在查看带有 ASP.NET 网页的 Razor 视图引擎的最新版本时,它看起来不错,因为它具有完整的 Razor 支持。那么有人可以给我一个理由,为什么我可能想要选择带有 Razor 视图引擎的 ASP.NET 网页而不是传统的 Web 表单,或其在 ASP.NET MVC 中的 Razor 视图引擎伴侣?
对于每个网页,我指的是 VS 2010 SP 1 中提供的新网站框架: http://www.asp.net/webmatrix/tutorials/2-introduction-to-asp-net-web-programming-using-the-razor-syntax
谢谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
razor 中的标记更具可读性。当您的产品已经使用 Web 表单视图引擎时,您可以在过渡到 razor 视图时与其并行运行 razor,这在中型到大型项目中可能需要大量工作。请记住,在此过渡期间,部分可以是 Razor 表单或 Web 表单,但视图及其使用的母版页必须使用相同的引擎。
The markup in razor is much more readable. When you have a product that already uses the web forms view engine, you can run razor side by side with it while you transition to razor views, which can be a lot of work in medium to larger sized projects. Keep in mind that during this transition, partials may be either razor or web forms, but a view and the master page it uses must use the same engine.
[已修改以回答标题中提出的问题]
作为一名经验丰富的开发人员,当您想要快速完成某件事(例如 POC),或者如果您想教某人如何开始使用时,您会使用网页框架也许是 ASP.NET。
您在这里问两个单独的问题:MVC 中的 Web 表单视图引擎与 Razor View Engine,以及网页框架与 Web 表单框架。正如其他人所说,第一个决定将取决于偏好。双方都不做对方不做的事情。这有点像 C# 或 VB 之争。Web Pages 框架旨在为初学者提供比 Web Forms 或 MVC 更容易的“入门”方式。它可以被认为是对更简单的经典 ASP 框架的一个非常迟来的替代品,并且在这方面它与 PHP 竞争。如果您需要分离、可测试性等,您应该考虑 MVC 框架。网页框架在企业开发方法方面根本没有提供太多东西。
习惯了 Web 表单,您不太可能从网页中获得任何东西。分离要困难得多。它不鼓励分层代码或测试。没有服务器控件或代码隐藏文件。而且网页仅提供网站部署(首次运行时编译),而不提供可以预编译的 Web 应用程序。Razor 本身是为网页框架开发的。 MVC 喜欢它并将其用作新的视图引擎选项。但本质上,您现在可以选择三种 ASP.NET 框架:网页、Web 窗体和 MVC。
[Modified to answer the question posed in the title]
As an experienced developer, you would use the Web Pages framework when you want to get something done quickly (POC, for example), or if you want to teach someone how to get started with ASP.NET, perhaps.
You are asking two separate questions here: Web Forms View Engine vs Razor View Engine in MVC, and Web Pages Framework vs Web Forms Framework.As others have said the first decision will be down to preference. Neither does anything that the other doesn't. It's a bit like the C# or VB debate.The Web Pages framework is intended to provide an easier "in" for beginners than Web Forms or MVC. It can be considered a very belated replacement for the much easier classic ASP framework and it competes with PHP in that respect. If you need separation, testability and so on you should look to the MVC framework. The Web Pages framework doesn't offer very much at all in terms of enterprise development approaches.
are used to Web Forms you are unlikely to gain anything from Web Pages. Separation is a lot more difficult. It doesn't encourage layering of code or testing. No server controls or code-behind files. And Web Pages only offers the Web Site deployment (compile on first run) as opposed to the Web Application which can be pre-compiled.Razor itself was developed for the Web Pages framework. MVC liked it and leveraged it as a new view engine option. But essentially, you have three ASP.NET frameworks to choose from now: Web Pages, Web Forms and MVC.
选择 Razor 的主要原因:
<%
和%>
。 (我喜欢干净的 HTML。它让我的强迫症发痒......)The main reasons to choose Razor:
<%
and%>
everywhere. (I like clean HTML. It tickles my OCD...)