我一直在 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.
优点:
缺点:
例子:
<%@ 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
设计目标:
优点:
缺点:
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.
优点:
缺点:
例子:
<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.
优点:
缺点:
例子:
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.
优点:
WebFormViewEngine
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.
优点:
缺点:
例子:
@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.
优点:
缺点:
例子:
#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).
优点:
缺点:
例子:
<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.
优点:
缺点:
例子:
<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.
优点:
缺点:
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.
优点:
缺点:
XsltViewEngine (MvcContrib)
设计目标:
Builds views from familiar XSLT
优点:
缺点:
关于asp.net-mvc - ASP.NET MVC View 引擎比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1451319/