views:

600

answers:

2

There are quite a lot of questions on SO regarding View Engines in ASP.Net MVC, and about using "custom" ones instead of the "default" one. For me as a hobby programmer, the term "View Engine" is new, and I have not been able to understand what it means. So, my questions are:

  1. What is a View Engine?

  2. What does the View Engine do, and which role in the MVC pattern does it play? (Closely related to 1...)

  3. What are the main characteristics/properties of the default View Engine that programmers want to change/avoid by switching to a different View Engine?

  4. What are the main benefits of common other View Engines out there that developers are after when they choose to use a different one than standard? (I've seen the name "Spark View Engine" a bunch of times, and I bet there are others too).

  5. When (in what scenarios) would I want to develop my own View Engine?

There, I think that is all I want to ask (for now). Give me View Engines 101! =)

+4  A: 

The "view engine" handles the rendering of the view to html, xml or whatever content type it is created to emit. Within "MVC", it would be an aspect of the View (V).

Different view engines have different syntaxes, etc. to manage rendering. The decision to use another view engine is most likely very project/programmer specific. In some cases they may see an actual or perceived limitation of the default view engine; in other cases it may simply be a different design goal or focus.

As far as Spark goes, their focus is to be much more terse than the default view engine and to remain in HTML-like syntax as much as possible instead of dropping into ASP.NET script blocks.

As an end user, the only time you would want to create your own view engine is probably never. ;) It's not a task to be taken lightly, and you'll probably end up re-implementing functionality that already exists in an existing view engine.

Edit

OK. So are the View.aspx files part of the View Engine, or is the View Engine a set of classes that help choose which View.aspx (or other type of response) that should be rendered? How does it work?

The MVC pattern tells you that your model, view and controller will be separate "things". In ASP.NET MVC, the default view engine uses the existing ASP.NET framework, which includes master pages, ASPX files, etc. Spark does something similar, but it's a different engine so it doesn't work exactly the same. So in a general sense the view files are not engine-specific, but the specific files, their layout on disk and their contents are view-engine specific.

GalacticCowboy
OK. So are the View.aspx files part of the View Engine, or is the View Engine a set of classes that help choose which View.aspx (or other type of response) that should be rendered? How does it work?
Tomas Lycken
A: 

In addition to wanting to completely replace the view engine (e.g. Spark), you also might make your own view engine just to make a small change to the behavior of the default view engine. You could do this by creating a new view engine that inherits from the default engine and overrides some behavior.

One common example is tweaking where the view engine looks on the hard drive for the .aspx files. For example, you might want to create a view engine that automatically switches which view.aspx file is used based on whether the user is on a mobile device.

See Mobile Web Sites with ASP.NET MVC and the Mobile Browser Definition File for a really good example implementation of this.

Erv Walter