Following the introduction of razor last week, ASP. NET MVC now has four main view engines. The other three engines are spark, NHAML, and legacy aspx file templates. This article will outline these four engines and focus on the new razor engine.
The ASPX-style view engine can be traced back to the remote ASP. Using <%=%> and <%: The placeholder for%> syntax occupies a dominant position in such styles. Over time, the ASPC control is added, followed by the master page, but it also brings an expensive page life cycle.
Spark, also used in the monorail of the Castle project, takes a slightly different approach. It supports both placeholders and the ability to mix HTML and code without restrictions with special attributes and tags. It may be cumbersome, but it has the advantage of being fully XML-compatible.
Nhaml is a. NET implementation of the Haml markup language. This language does not use XML-style syntax at all. With a shorthand syntax, it eliminates a lot of syntactic noise in the previous style (XML), such as ending tags.
The latest view engine is Microsoft's Razor. While all templating systems have some common features, Razor is quite different from the three view engines we discussed earlier. Unlike Nhaml,razor, XML tags are still used, but unlike Spark,razor, which does not go so far in terms of using XML code. It is also not exactly like ASPX, because it replaces the more cumbersome placeholders with the @ sign-on expression or the normal control block. Because no special closing tag is required, the final code of razor is concise.
By default, all text generated from the @ expression is HTML-encoded "to prevent XSS attacks." The text inside the @ block can contain multiple "HTML content blocks". When any XML tag is identified, razor switches back to HTML mode, very similar to the technique used by traditional ASP developers to "%> HTML code <%=". If you actually don't want the XML tag to switch back to HTML mode, you can use <text> as a placeholder.
Because Razor uses the existing VB or C # syntax, Microsoft expects it to be easy to learn. Any text editor can be used to edit the razor file, and Visual Studio 2010 will also add updates to the full support for Razor file smart hints.
Another important feature of Razor is its compatibility with the unit test framework. The razor template does not require a controller or Web server as host (host), so the view written out with it should be fully measurable. For ASPX, although theoretically everything can be tested, it is actually quite difficult, while the other two engines don't even really mention testability on their websites.
Four view engines support the concept of master pages, so there's not much to talk about here. ASPX provides only partial support for precompiling views into assembly files, while Razor and spark are fully supported. Nhaml currently puts this in the list of requirements features. It is particularly interesting that razor and spark pre-compiled views can be used in a non-hosted scenario, such as a mail merge engine.
View English Original : Four view Engines for ASP.
ASP. NET MVC View engine