asp.net-mvc ASP.NET MVC 视图引擎比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1451319/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:51:29  来源:igfitidea点击:

ASP.NET MVC View Engine Comparison

asp.net-mvcspark-view-engineviewenginerazor

提问by mckamey

I've been searching on SO & Google for a breakdown of the various View Engines available for ASP.NET MVC, but haven't found much more than simple high-level descriptions of what a view engine is.

我一直在 SO 和 Google 上搜索可用于 ASP.NET MVC 的各种视图引擎的细分,但除了对视图引擎是什么的简单高级描述之外,没有找到更多内容。

I'm not necessarily looking for "best" or "fastest" but rather some real world comparisons of advantages / disadvantages of the major players (e.g. the default WebFormViewEngine, MvcContrib View Engines, etc.) for various situations. I think this would be really helpful in determining if switching from the default engine would be advantageous for a given project or development group.

我不一定要寻找“最佳”或“最快”,而是在各种情况下对主要参与者(例如默认的 WebFormViewEngine、MvcContrib 视图引擎等)的优势/劣势进行一些现实世界的比较。我认为这将有助于确定从默认引擎切换是否对给定的项目或开发组有利。

Has anyone encountered such a comparison?

有没有人遇到过这样的比较?

回答by mckamey

ASP.NET MVC View Engines (Community Wiki)

ASP.NET MVC 视图引擎(社区 Wiki)

Since a comprehensive list does not appear to exist, let's start one here on SO. This can be of great value to the ASP.NET MVC community if people add their experience (esp. anyone who contributed to one of these). Anything implementing IViewEngine(e.g. VirtualPathProviderViewEngine) is fair game here. Just alphabetize new View Engines (leaving WebFormViewEngine and Razor at the top), and try to be objective in comparisons.

由于似乎不存在完整的列表,让我们从 SO 开始。如果人们添加他们的经验(尤其是对其中之一做出贡献的任何人),这对 ASP.NET MVC 社区可能具有重要价值。任何实现IViewEngine(例如VirtualPathProviderViewEngine)在这里都是公平的游戏。只需按字母顺序排列新的视图引擎(将 WebFormViewEngine 和 Razor 留在顶部),并尝试在比较中保持客观。



System.Web.Mvc.WebFormViewEngine

System.Web.Mvc.WebFormViewEngine

Design Goals:

设计目标:

A view engine that is used to render a Web Forms page to the response.

用于将 Web 窗体页面呈现给响应的视图引擎。

Pros:

优点:

  • ubiquitous since it ships with ASP.NET MVC
  • familiar experience for ASP.NET developers
  • IntelliSense
  • can choose any language with a CodeDom provider (e.g. C#, VB.NET, F#, Boo, Nemerle)
  • on-demand compilation or precompiledviews
  • 无处不在,因为它随 ASP.NET MVC 一起提供
  • ASP.NET 开发人员的熟悉体验
  • 智能感知
  • 可以选择任何带有 CodeDom 提供程序的语言(例如 C#、VB.NET、F#、Boo、Nemerle)
  • 按需编译或预编译视图

Cons:

缺点:

  • usage is confused by existence of "classic ASP.NET" patterns which no longer apply in MVC (e.g. ViewState PostBack)
  • can contribute to anti-pattern of "tag soup"
  • code-block syntax and strong-typing can get in the way
  • IntelliSense enforces style not always appropriate for inline code blocks
  • can be noisy when designing simple templates
  • 用法被不再适用于 MVC 的“经典 ASP.NET”模式的存在所混淆(例如 ViewState PostBack)
  • 可以促成“标签汤”的反模式
  • 代码块语法和强类型可能会妨碍
  • IntelliSense 强制执行的样式并不总是适用于内联代码块
  • 设计简单模板时可能会很吵

Example:

例子:

<%@ Control Inherits="System.Web.Mvc.ViewPage<IEnumerable<Product>>" %>
<% if(model.Any()) { %>
<ul>
    <% foreach(var p in model){%>
    <li><%=p.Name%></li>
    <%}%>
</ul>
<%}else{%>
    <p>No products available</p>
<%}%>


System.Web.Razor

系统.Web.Razor

Design Goals:

设计目标:

Pros:

优点:

  • Compact, Expressive, and Fluid
  • Easy to Learn
  • Is not a new language
  • Has great Intellisense
  • Unit Testable
  • Ubiquitous, ships with ASP.NET MVC
  • 紧凑、富有表现力和流畅
  • 简单易学
  • 不是一门新语言
  • 有很好的智能感知
  • 单元可测试
  • 无处不在,随 ASP.NET MVC 一起提供

Cons:

缺点:

  • Creates a slightly different problem from "tag soup" referenced above. Where the server tags actually provide structure around server and non-server code, Razor confuses HTML and server code, making pure HTML or JS development challenging (see Con Example #1) as you end up having to "escape" HTML and / or JavaScript tags under certain very common conditions.
  • Poor encapsulation+reuseability: It's impractical to call a razor template as if it were a normal method - in practice razor can call code but not vice versa, which can encourage mixing of code and presentation.
  • Syntax is very html-oriented; generating non-html content can be tricky. Despite this, razor's data model is essentially just string-concatenation, so syntax and nesting errors are neither statically nor dynamically detected, though VS.NET design-time help mitigates this somewhat. Maintainability and refactorability can suffer due to this.
  • No documented API, http://msdn.microsoft.com/en-us/library/system.web.razor.aspx
  • 创建一个与上面引用的“标签汤”略有不同的问题。服务器标签实际上提供了围绕服务器和非服务器代码的结构,而 Razor 混淆了 HTML 和服务器代码,使纯 HTML 或 JS 开发变得具有挑战性(参见 Con Example #1),因为您最终不得不“转义”HTML 和/或 JavaScript标签在某些非常常见的条件下。
  • 较差的封装性+可重用性:将 razor 模板当作普通方法来调用是不切实际的——实际上 razor 可以调用代码,反之则不行,这会鼓励代码和演示的混合。
  • 语法非常面向 html;生成非 html 内容可能很棘手。尽管如此,razor 的数据模型本质上只是字符串连接,所以语法和嵌套错误既不是静态也不是动态检测,尽管 VS.NET 设计时帮助在某种程度上缓解了这一点。因此,可维护性和可重构性可能会受到影响。
  • 没有记录的 APIhttp://msdn.microsoft.com/en-us/library/system.web.razor.aspx

Con Example #1 (notice the placement of "string[]..."):

Con Example #1(注意“string[]...”的位置):

@{
    <h3>Team Members</h3> string[] teamMembers = {"Matt", "Joanne", "Robert"};
    foreach (var person in teamMembers)
    {
        <p>@person</p>
    }
}


Bellevue

贝尔维尤

Design goals:

设计目标:

  • Respect HTML as first-class language as opposed to treating it as "just text".
  • Don't mess with my HTML! The data binding code (Bellevue code) should be separate from HTML.
  • Enforce strict Model-View separation
  • 将 HTML 视为一流语言,而不是将其视为“纯文本”。
  • 不要弄乱我的 HTML!数据绑定代码(Bellevue 代码)应该与 HTML 分开。
  • 强制执行严格的模型-视图分离


Brail

抄网

Design Goals:

设计目标:

The Brail view engine has been ported from MonoRail to work with the Microsoft ASP.NET MVC Framework. For an introduction to Brail, see the documentation on the Castle project website.

Brail 视图引擎已从 MonoRail 移植到 Microsoft ASP.NET MVC 框架中。有关 Brail 的介绍,请参阅Castle 项目网站上的文档。

Pros:

优点:

  • modeled after "wrist-friendly python syntax"
  • On-demand compiled views (but no precompilation available)
  • 仿照“手腕友好的python语法”
  • 按需编译视图(但没有预编译可用)

Cons:

缺点:

  • designed to be written in the language Boo
  • 设计为用Boo语言编写

Example:

例子:

<html>    
<head>        
<title>${title}</title>
</head>    
<body>        
     <p>The following items are in the list:</p>  
     <ul><%for element in list:    output "<li>${element}</li>"%></ul>
     <p>I hope that you would like Brail</p>    
</body>
</html>


Hasic

哈西奇

Hasic uses VB.NET's XML literals instead of strings like most other view engines.

Hasic 使用 VB.NET 的 XML 文字而不是像大多数其他视图引擎一样的字符串。

Pros:

优点:

  • Compile-time checking of valid XML
  • Syntax colouring
  • Full intellisense
  • Compiled views
  • Extensibility using regular CLR classes, functions, etc
  • Seamless composability and manipulation since it's regular VB.NET code
  • Unit testable
  • 有效 XML 的编译时检查
  • 语法着色
  • 全智能感知
  • 编译视图
  • 使用常规 CLR 类、函数等的可扩展性
  • 无缝组合和操作,因为它是常规的 VB.NET 代码
  • 单元可测试

Cons:

缺点:

  • Performance: Builds the whole DOM before sending it to client.
  • 性能:在将其发送到客户端之前构建整个 DOM。

Example:

例子:

Protected Overrides Function Body() As XElement
    Return _
    <body>
        <h1>Hello, World</h1>
    </body>
End Function


NDjango

姜戈

Design Goals:

设计目标:

NDjango is an implementation of the Django Template Languageon the .NET platform, using the F# language.

NDjango 是.NET 平台上Django 模板语言的实现 ,使用F# 语言

Pros:

优点:



NHaml

哈姆勒

Design Goals:

设计目标:

.NET port of Rails Haml view engine. From the Haml website:

Haml is a markup language that's used to cleanly and simply describe the XHTML of any web document, without the use of inline code... Haml avoids the need for explicitly coding XHTML into the template, because it is actually an abstract description of the XHTML, with some code to generate dynamic content.

Rails Haml 视图引擎的 .NET 端口。来自哈姆勒网站

Haml 是一种标记语言,用于干净、简单地描述任何 Web 文档的 XHTML,而不使用内联代码...... Haml 避免了将 XHTML 显式编码到模板中的需要,因为它实际上是 XHTML 的抽象描述,用一些代码来生成动态内容。

Pros:

优点:

  • terse structure (i.e. D.R.Y.)
  • well indented
  • clear structure
  • C# Intellisense(for VS2008 without ReSharper)
  • 简洁的结构(即 DRY)
  • 缩进良好
  • 结构清晰
  • C# Intellisense(适用于不带 ReSharper 的 VS2008)

Cons:

缺点:

  • an abstraction from XHTML rather than leveraging familiarity of the markup
  • No Intellisense for VS2010
  • 对 XHTML 的抽象,而不是利用熟悉的标记
  • VS2010 没有智能感知

Example:

例子:

@type=IEnumerable<Product>
- if(model.Any())
  %ul
    - foreach (var p in model)
      %li= p.Name
- else
  %p No products available


NVelocityViewEngine (MvcContrib)

NVelocityViewEngine (MvcContrib)

Design Goals:

设计目标:

A view engine based upon NVelocitywhich is a .NET port of the popular Java project Velocity.

基于NVelocity 的视图引擎, 它是流行的 Java 项目Velocity的 .NET 端口 。

Pros:

优点:

  • easy to read/write
  • concise view code
  • 易于读/写
  • 简洁的视图代码

Cons:

缺点:

  • limited number of helper methods available on the view
  • does not automatically have Visual Studio integration (IntelliSense, compile-time checking of views, or refactoring)
  • 视图上可用的辅助方法数量有限
  • 不会自动集成 Visual Studio(智能感知、视图的编译时检查或重构)

Example:

例子:

#foreach ($p in $viewdata.Model)
#beforeall
    <ul>
#each
    <li>$p.Name</li>
#afterall
    </ul>
#nodata 
    <p>No products available</p>
#end


SharpTiles

夏普

Design Goals:

设计目标:

SharpTiles is a partial port of JSTLcombined with concept behind the Tiles framework(as of Mile stone 1).

SharpTiles 是JSTL的部分端口, 结合了Tiles 框架背后的概念(从里程碑 1 开始)。

Pros:

优点:

  • familiar to Java developers
  • XML-style code blocks
  • Java 开发人员熟悉
  • XML 样式的代码块

Cons:

缺点:

  • ...
  • ...

Example:

例子:

<c:if test="${not fn:empty(Page.Tiles)}">
  <p class="note">
    <fmt:message key="page.tilesSupport"/>
  </p>
</c:if>


Spark View Engine

Spark 视图引擎

Design Goals:

设计目标:

The idea is to allow the html to dominate the flow and the code to fit seamlessly.

这个想法是让 html 主宰流程,让代码无缝地适应。

Pros:

优点:

  • Produces more readable templates
  • C# Intellisense(for VS2008 without ReSharper)
  • SparkSense plug-infor VS2010 (works with ReSharper)
  • Provides a powerful Bindings featureto get rid of allcode in your views and allows you to easily invent your own HTML tags
  • 生成更具可读性的模板
  • C# Intellisense(适用于不带 ReSharper 的 VS2008)
  • VS2010 的SparkSense 插件(适用于 ReSharper)
  • 提供强大的绑定功能来摆脱视图中的所有代码,并允许您轻松发明自己的 HTML 标签

Cons:

缺点:

  • No clear separation of template logic from literal markup (this can be mitigated by namespace prefixes)
  • 模板逻辑与文字标记没有明确的分离(这可以通过命名空间前缀来缓解)

Example:

例子:

<viewdata products="IEnumerable[[Product]]"/>
<ul if="products.Any()">
    <li each="var p in products">${p.Name}</li>
</ul>
<else>
    <p>No products available</p>
</else>

<Form style="background-color:olive;">
    <Label For="username" />
    <TextBox For="username" />
    <ValidationMessage For="username" Message="Please type a valid username." />
</Form>


StringTemplate View Engine MVC

StringTemplate 视图引擎 MVC

Design Goals:

设计目标:

  • Lightweight. No page classes are created.
  • Fast. Templates are written to the Response Output stream.
  • Cached. Templates are cached, but utilize a FileSystemWatcher to detect file changes.
  • Dynamic. Templates can be generated on the fly in code.
  • Flexible. Templates can be nested to any level.
  • In line with MVC principles. Promotes separation of UI and Business Logic. All data is created ahead of time, and passed down to the template.
  • 轻的。没有创建页面类。
  • 快速地。模板被写入响应输出流。
  • 缓存。模板被缓存,但利用 FileSystemWatcher 来检测文件更改。
  • 动态的。模板可以在代码中即时生成。
  • 灵活的。模板可以嵌套到任何级别。
  • 符合MVC原则。促进 UI 和业务逻辑的分离。所有数据都是提前创建的,并传递给模板。

Pros:

优点:

  • familiar to StringTemplate Java developers
  • 熟悉 StringTemplate Java 开发人员

Cons:

缺点:

  • simplistic template syntax can interfere with intended output (e.g. jQuery conflict)
  • 简单的模板语法会干扰预期的输出(例如jQuery 冲突


Wing Beats

翼拍

Wing Beats is an internal DSL for creating XHTML. It is based on F# and includes an ASP.NET MVC view engine, but can also be used solely for its capability of creating XHTML.

Wing Beats 是用于创建 XHTML 的内部 DSL。它基于 F# 并包含一个 ASP.NET MVC 视图引擎,但也可以单独用于创建 XHTML 的功能。

Pros:

优点:

  • Compile-time checking of valid XML
  • Syntax colouring
  • Full intellisense
  • Compiled views
  • Extensibility using regular CLR classes, functions, etc
  • Seamless composability and manipulation since it's regular F# code
  • Unit testable
  • 有效 XML 的编译时检查
  • 语法着色
  • 全智能感知
  • 编译视图
  • 使用常规 CLR 类、函数等的可扩展性
  • 无缝组合和操作,因为它是常规的 F# 代码
  • 单元可测试

Cons:

缺点:

  • You don't really write HTML but code that represents HTML in a DSL.
  • 您实际上并不是在编写 HTML,而是在 DSL 中编写表示 HTML 的代码。


XsltViewEngine (MvcContrib)

XsltViewEngine (MvcContrib)

Design Goals:

设计目标:

Builds views from familiar XSLT

从熟悉的 XSLT 构建视图

Pros:

优点:

  • widely ubiquitous
  • familiar template language for XML developers
  • XML-based
  • time-tested
  • Syntax and element nesting errors can be statically detected.
  • 无处不在
  • XML 开发人员熟悉的模板语言
  • 基于 XML
  • 久经考验
  • 可以静态检测语法和元素嵌套错误。

Cons:

缺点:

  • functional language style makes flow control difficult
  • XSLT 2.0 is (probably?) not supported. (XSLT 1.0 is much less practical).
  • 函数式语言风格使流程控制变得困难
  • XSLT 2.0(可能?)不受支持。(XSLT 1.0 不太实用)。


回答by nathj07

My current choice is Razor. It is very clean and easy to read and keeps the view pages very easy to maintain. There is also intellisense support which is really great. ALos, when used with web helpers it is really powerful too.

我目前的选择是剃刀。它非常干净且易于阅读,并使视图页面非常易于维护。还有非常棒的智能感知支持。ALos,当与网络助手一起使用时,它也非常强大。

To provide a simple sample:

提供一个简单的示例:

@Model namespace.model
<!Doctype html>
<html>
<head>
<title>Test Razor</title>
</head>
<body>
<ul class="mainList">
@foreach(var x in ViewData.model)
{
<li>@x.PropertyName</li>
}
</ul>
</body>

And there you have it. That is very clean and easy to read. Granted, that's a simple example but even on complex pages and forms it is still very easy to read and understand.

你有它。这是非常干净和易于阅读的。当然,这是一个简单的例子,但即使在复杂的页面和表单上,它仍然很容易阅读和理解。

As for the cons? Well so far (I'm new to this) when using some of the helpers for forms there is a lack of support for adding a CSS class reference which is a little annoying.

至于利弊?到目前为止(我是新来的)当使用一些表单的助手时,缺乏对添加 CSS 类引用的支持,这有点烦人。

Thanks Nathj07

谢谢 Nathj07

回答by MunkiPhD

I know this doesn't really answer your question, but different View Engines have different purposes. The Spark View Engine, for example, aims to rid your views of "tag soup" by trying to make everything fluent and readable.

我知道这并不能真正回答您的问题,但不同的视图引擎有不同的目的。在星火视图引擎,例如,目的是试图让一切流畅可读摆脱你的“标签汤”的观点。

Your best bet would be to just look at some implementations. If it looks appealing to the intent of your solution, try it out. You can mix and match view engines in MVC, so it shouldn't be an issue if you decide to not go with a specific engine.

最好的办法是只看一些实现。如果它看起来对您的解决方案的意图很有吸引力,请尝试一下。您可以在 MVC 中混合和匹配视图引擎,因此如果您决定不使用特定引擎,这应该不是问题。

回答by Anton Shelin

Check this SharpDOM. This is a c# 4.0 internal dsl for generating html and also asp.net mvc view engine.

检查这个SharpDOM。这是用于生成 html 和 asp.net mvc 视图引擎的 ac# 4.0 内部 dsl。

回答by rdovhan

I like ndjango. It is very easy to use and very flexible. You can easily extend view functionality with custom tags and filters. I think that "greatly tied to F#" is rather advantage than disadvantage.

我喜欢ndjango。它非常易于使用且非常灵活。您可以使用自定义标签和过滤器轻松扩展视图功能。我认为“与 F# 密切相关”与其说是缺点,不如说是优点。

回答by mythz

I think this list should also include samples of each view engine so users can get a flavour of each without having to visit every website.

我认为这个列表还应该包括每个视图引擎的示例,以便用户无需访问每个网站就可以了解每个引擎的风格。

Pictures say a thousand words and markup samples are like screenshots for view engines :) So here's one from my favourite Spark View Engine

图片说明了一千个单词,标记示例就像视图引擎的屏幕截图 :) 所以这是我最喜欢的Spark 视图引擎的一个

<viewdata products="IEnumerable[[Product]]"/>
<ul if="products.Any()">
  <li each="var p in products">${p.Name}</li>
</ul>
<else>
  <p>No products available</p>
</else>