The View Engine

Scott Hanselman, community program manager at Microsoft, likes to call the view engine “just an angle bracket generator.” In simplest terms, that's exactly what it is. A view engine will take an in-memory representation of a view and turn it into whatever other format you like. Usually, this means that you will create a CSHTML file containing markup and script, and ASP.NET MVC's default view engine implementation, the RazorViewEngine, will use some existing ASP.NET APIs to render your page as HTML.

View engines aren't limited to using CSHTML pages, nor are they limited to rendering HTML. You'll see later how you can create alternate view engines that render output that isn't HTML, as well as unusual view engines that take a custom DSL (Domain Specific Language) as input.

To better understand what a view engine is, let's review the ASP.NET MVC life cycle (very simplified in Figure 3.6).

A lot more subsystems are involved than Figure 3.6 shows; this figure just highlights where the view engine comes into play—which is right after the Controller action is executed and returns a ViewResult in response to a request.

It is very important to note here that the Controller itself does not render the view; it simply prepares the data (that is, the model) and decides which view to display by returning a ViewResult instance. As you saw earlier in this chapter, the ...

Get Professional ASP.NET MVC 3 now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.