Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

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

Suggested Videos 
Part 28 - Customizing the auto-generated edit view
Part 29 - Using data transfer object as the model
Part 30 - View engines

Out of the box ASP.NET MVC supports the following 2 view engines
1. ASPX
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" 
  ViewFileExtension=".spark" 
  DefaultLayoutPage="~/Views/Shared/_Layout.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.

2 comments:

  1. sir i'm getting error higher version Assembly
    VS2013 MVC 4.0.0.0
    Spark 1.8.0.0

    Error 2 Assembly 'Spark.Web.Mvc, Version=1.8.0.0, Culture=neutral, PublicKeyToken=7f8549eed921a12c' uses 'System.Web.Mvc, Version=4.0.0.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35' which has a higher version than referenced assembly 'System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' c:\Users\VIKAS\Documents\Visual Studio 2013\Projects\MvcDemo\packages\Spark.Web.Mvc4.1.8.1.0\lib\NET4\Spark.Web.Mvc.dll MvcDemo

    ReplyDelete
  2. I also got same error. So i downloaded Spark.Web.Mvc2 version from Nuget,at that time i am able to build the solution Successfully.

    ReplyDelete

It would be great if you can help share these free resources