asp.net-mvcspark-view-engineviewenginerazor

ASP.NET MVC View Engine Comparison


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.

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?


Solution

  • 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

    Design Goals:

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

    Pros:

    Cons:

    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

    Design Goals:

    Pros:

    Cons:

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

    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

    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.

    Pros:

    Cons:

    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.

    Pros:

    Cons:

    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 Language on the .NET platform, using the F# language.

    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.

    Pros:

    Cons:

    Example:

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

    NVelocityViewEngine (MvcContrib)

    Design Goals:

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

    Pros:

    Cons:

    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 JSTL combined with concept behind the Tiles framework (as of Mile stone 1).

    Pros:

    Cons:

    Example:

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

    Spark View Engine

    Design Goals:

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

    Pros:

    Cons:

    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

    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.

    Pros:

    Cons:


    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.

    Pros:

    Cons:


    XsltViewEngine (MvcContrib)

    Design Goals:

    Builds views from familiar XSLT

    Pros:

    Cons: