What is an ASP.Net MVC View Engine?
Asked Answered
L

2

15

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! =)

Lynxeyed answered 12/5, 2009 at 14:32 Comment(1)
If this should be community wiki, please let me know.Lynxeyed
O
6

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.

Oxpecker answered 12/5, 2009 at 15:7 Comment(1)
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?Lynxeyed
B
1

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.

Update:

Scott just posted an updated ViewEngine implementation that has important improvements vs the initial implementation linked above.

Bagley answered 12/5, 2009 at 15:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.