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

标签 asp.net-mvc spark-view-engine viewengine razor

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

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

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

最佳答案

ASP.NET MVC View 引擎(社区 Wiki)

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

System.Web.Mvc.WebFormViewEngine

设计目标:

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



优点:
  • 无处不在,因为它随 ASP.NET MVC 一起提供
  • ASP.NET 开发人员的熟悉体验
  • 智能感知
  • 可以选择任何带有 CodeDom 提供程序的语言(例如 C#、VB.NET、F#、Boo、Nemerle)
  • 按需编译或precompiled浏览次数

  • 缺点:
  • 用法被不再适用于 MVC 的“经典 ASP.NET”模式所混淆(例如 ViewState PostBack)
  • 有助于“标签汤”的反模式
  • 代码块语法和强类型可能会成为障碍
  • IntelliSense 强制执行的样式并不总是适用于内联代码块
  • 设计简单模板时可能会很吵

  • 例子:
    <%@ 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

    设计目标:

    优点:
  • 紧凑、富有表现力和流畅
  • 简单易学
  • 不是新语言
  • 有很好的智能感知
  • 单元可测试
  • 无处不在,随 ASP.NET MVC 一起提供

  • 缺点:
  • 创建一个与上面引用的“标签汤”略有不同的问题。服务器标签实际上提供了围绕服务器和非服务器代码的结构,而 Razor 混淆了 HTML 和服务器代码,使纯 HTML 或 JS 开发变得具有挑战性(参见 Con Example #1),因为您最终不得不“转义”HTML 和/或 JavaScript标签在某些非常常见的条件下。
  • 较差的封装性+可重用性:将 razor 模板当作普通方法来调用是不切实际的——实际上 razor 可以调用代码,但反之则不行,这会鼓励代码和演示的混合。
  • 语法非常面向 html;生成非 html 内容可能很棘手。尽管如此,razor 的数据模型本质上只是字符串连接,因此语法和嵌套错误既不能静态检测也不能动态检测,尽管 VS.NET 设计时帮助缓解了这种情况。因此,可维护性和可重构性可能会受到影响。
  • 没有记录的 API,http://msdn.microsoft.com/en-us/library/system.web.razor.aspx

  • Con Example #1(注意“string[]...”的位置):
    @{
        <h3>Team Members</h3> string[] teamMembers = {"Matt", "Joanne", "Robert"};
        foreach (var person in teamMembers)
        {
            <p>@person</p>
        }
    }
    

    Bellevue

    设计目标:

    • 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


    Brail

    设计目标:

    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.



    优点:
  • 仿照“手腕友好的 Python 语法”
  • 按需编译 View (但没有预编译可用)

  • 缺点:
  • 旨在用语言编写 Boo

  • 例子:
    <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.



    优点:
  • 有效 XML 的编译时检查
  • 语法着色
  • 全智能感知
  • 编译 View
  • 使用常规 CLR 类、函数等的可扩展性
  • 无缝组合和操作,因为它是常规的 VB.NET 代码
  • 单元可测试

  • 缺点:
  • 性能:在将其发送到客户端之前构建整个 DOM。

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

    NDjango

    设计目标:

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



    优点:
  • NDjango release 0.9.1.0 seems to be more stable under stress than WebFormViewEngine
  • Django 模板编辑器,具有语法着色、代码完成和即时诊断功能(仅限 VS2010)
  • 与 ASP.NET、CaSTLe MonoRail 和 Bistro MVC 框架集成


  • NHaml

    设计目标:

    .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.



    优点:
  • 简洁的结构(即 D.R.Y.)
  • 缩进良好
  • 结构清晰
  • C# Intellisense (对于没有 ReSharper 的 VS2008)

  • 缺点:
  • XHTML 的抽象,而不是利用熟悉的标记
  • VS2010 没有智能感知

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

    NVelocityViewEngine (MvcContrib)

    设计目标:

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



    优点:
  • 易读/写
  • 简洁查看代码

  • 缺点:
  • View 上可用的辅助方法数量有限
  • 不会自动集成 Visual Studio(智能感知、 View 的编译时检查或重构)

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

    SharpTiles

    设计目标:

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



    优点:
  • Java 开发人员熟悉
  • XML 样式的代码块

  • 缺点:
  • ...

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

    Spark View Engine

    设计目标:

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



    优点:
  • 生成更具可读性的模板
  • C# Intellisense (对于没有 ReSharper 的 VS2008)
  • SparkSense plug-in适用于 VS2010(适用于 ReSharper)
  • 提供强大的Bindings feature删除 View 中的所有代码,并允许您轻松发明自己的 HTML 标签

  • 缺点:
  • 模板逻辑与文字标记没有明确的分离(这可以通过命名空间前缀来缓解)

  • 例子:
    <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

    设计目标:

    • 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.


    优点:
  • StringTemplate Java 开发人员熟悉

  • 缺点:
  • 简单的模板语法可能会干扰预期的输出(例如 jQuery conflict )


  • 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.



    优点:
  • 有效 XML 的编译时检查
  • 语法着色
  • 全智能感知
  • 编译 View
  • 使用常规 CLR 类、函数等的可扩展性
  • 无缝组合和操作,因为它是常规 F# 代码
  • 单元可测试

  • 缺点:
  • 您实际上并不是在编写 HTML,而是在 DSL 中编写表示 HTML 的代码。


  • XsltViewEngine (MvcContrib)

    设计目标:

    Builds views from familiar XSLT



    优点:
  • 无处不在
  • XML 开发人员熟悉的模板语言
  • 基于 XML
  • 久经考验
  • 可以静态检测语法和元素嵌套错误。

  • 缺点:
  • 函数式语言风格使流程控制变得困难
  • XSLT 2.0(可能?)不受支持。 (XSLT 1.0 不太实用)。
  • 关于asp.net-mvc - ASP.NET MVC View 引擎比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1451319/

    相关文章:

    asp.net-mvc - 在 ASP.NET MVC Html.ActionLink 中包含 anchor 标记

    asp.net-mvc-3 - 为 Ajax.BeginForm 添加 Spark 绑定(bind)

    asp.net - 无法设置 asp.net mvc 2 RC 和 Spark View 引擎

    asp.net - _ViewStart 未在自定义 ViewEngine 后使用

    asp.net-mvc - ASP.Net MVC 认证

    c# - 在 ASP.MVC 中创建一个通用的 NotFound View

    asp.net-mvc-3 - ReleaseView什么时候被调用?

    node.js - 如何在express js中授予用户特定的文件访问权限

    javascript - 动态添加字段并发送JSON

    django - 与(ASP.net MVC、NHibernate 和 Spark View Engine)相比,使用 Django 的缺点是什么?