Part 31 - Using custom view engines with asp.net mvc

Leave a Comment
Out of the box ASP.NET MVC supports the following 2 view engines
2. Razor

In addition to the above 2 view engines, there are several custom view engines that can be used with asp.net mvc. The following are a few of these custom view engines.
1. Spark
2. NHaml
3. SharpDOM
4. Brail etc....

For example, if you want to use Spark as the view engine for your asp.net mvc 4 project, then install Spark.Web.Mvc4 using NuGet Package Manager.

At this point, right click on any controller action method and select "Add View" from the context menu. Notice that, "View Engine" dropdownlist in "Add View" dialog box only shows - Razor and ASPX view engines. If you want the "Spark" view engine to be listed, then, we need to register "Spark View Engine" templates. 

Step 1: Navigate to the following path and create a folder with name="Spark". Spark specific templates must be copied into "Spark" folder.
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 4\CodeTemplates\AddView

Step 2: Create an xml file with in "Spark" folder. The name of the XML file must be "ViewEngine.xml" and must contain the following xml content.
<?xml version="1.0" encoding="utf-8" ?>
<ViewEngine DisplayName="Spark" 
  PartialViewFileExtension=".spark" />

Now, right click on any controller action method and select "Add View" from the context menu. Notice that, "Spark" view engine is also listed.


Post a Comment

Note: only a member of this blog may post a comment.