ASP.NET MVC 视图引擎比较 [英] ASP.NET MVC View Engine Comparison

查看:36
本文介绍了ASP.NET MVC 视图引擎比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

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

Has anyone encountered such a comparison?

推荐答案

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

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

ASP.NET MVC View Engines (Community 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.

System.Web.Mvc.WebFormViewEngine

设计目标:

一个视图引擎,用于渲染一个响应的 Web 表单页面.

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)
  • 按需编译或预编译视图

缺点:

  • 由于存在不再适用于 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 一起提供

缺点:

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

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

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

<小时>

贝尔维尤

设计目标:

  • 将 HTML 视为一流语言,而不是将其视为纯文本".
  • 不要弄乱我的 HTML!数据绑定代码(Bellevue 代码)应该与 HTML 分开.
  • 强制执行严格的模型-视图分离

<小时>

盲文

设计目标:

Brail 视图引擎已被移植从 MonoRail 到与微软 ASP.NET MVC 框架.为了Brail 简介,请参阅Castle 项目的文档网站.

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 语法"
  • 按需编译视图(但没有预编译可用)

缺点:

  • 设计为使用 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 使用 VB.NET 的 XML 文字而不是像大多数其他视图引擎一样的字符串.

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

优点:

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

缺点:

  • 性能:在将其发送给客户端之前构建整个 DOM.

示例:

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

<小时>

NDjango

设计目标:

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

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

优点:

NHaml

设计目标:

Rails Haml 视图引擎的 .NET 端口.来自 Haml 网站:

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

Haml 是一种使用的标记语言简洁明了地描述任何 Web 文档的 XHTML,没有使用内联代码... Haml 避免了需要将 XHTML 显式编码为模板,因为它实际上是XHTML 的抽象描述,用一些代码来生成动态内容.

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)
  • terse structure (i.e. D.R.Y.)
  • well indented
  • clear structure
  • C# Intellisense (for VS2008 without ReSharper)

缺点:

  • 对 XHTML 的抽象,而不是利用熟悉的标记
  • 没有适用于 VS2010 的智能感知

示例:

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

<小时>

NVelocityViewEngine (MvcContrib)

设计目标:

基于的视图引擎NVelocity 这是一个 .NET 端口流行的 Java 项目速度.

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

优点:

  • 易于读/写
  • 简洁的视图代码

缺点:

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

示例:

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

<小时>

SharpTiles

设计目标:

SharpTiles 是 JSTL 的部分移植结合 Tiles 背后的概念框架(从里程碑 1 开始).

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 视图引擎

设计目标:

这个想法是让html控制流程和代码以适应无缝衔接.

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

优点:

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

缺点:

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

示例:

<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 视图引擎 MVC

设计目标:

  • 轻量级.没有创建页面类.
  • 快.模板被写入响应输出流.
  • 缓存.模板被缓存,但利用 FileSystemWatcher 来检测文件更改.
  • 动态.可以在代码中动态生成模板.
  • 灵活.模板可以嵌套到任何级别.
  • 符合 MVC 原则.促进 UI 和业务的分离逻辑.所有数据都是提前创建的时间,并传递给模板.

优点:

  • 熟悉 StringTemplate Java 开发人员

缺点:

  • 简单的模板语法会干扰预期的输出(例如 jQuery 冲突)

Wing Beats

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

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
  • 语法着色
  • 全智能感知
  • 编译视图
  • 使用常规 CLR 类、函数等进行扩展
  • 无缝组合和操作,因为它是常规的 F# 代码
  • 可单元测试

缺点:

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

XsltViewEngine (MvcContrib)

设计目标:

从熟悉的 XSLT 构建视图

Builds views from familiar XSLT

优点:

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

缺点:

  • 函数式语言风格使流程控制变得困难
  • (可能?)不支持 XSLT 2.0.(XSLT 1.0 不太实用).

这篇关于ASP.NET MVC 视图引擎比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆