Adding Spark to ASP.NET MVC 2.0

After almost a year’s curiosity, with other stuff stealing my time, I have finally some time to have a look at the Spark View Engine. Since the Razor View Engine will be shipped together with ASP.NET MVC 3.0, I decided to give Spark a shot before Razor.

I will not go into detail about what Spark is, what it does etc. etc. The official web site has some good info and this blog helped me get started. I will just describe how to get Spark up and running and get it to play alongside a default ASP.NET MVC application.

Get Spark up and running

The first thing we have to do is to add Spark to our application. Visit the Spark web site, download and unzip the latest release, add it to your solution and add references to Spark.dll and Spark.Web.Mvc.dll.

Of course, if you use NuGet, just right-click the references folder and choose Add package reference…

…then search for spark and install sparkmvc.

This will automatically add most of what you need. Really convenient!

However, for Spark to work, we need to register it when the application starts. Do this by adding the following to Application_Start in global.asax.cs:

   ViewEngines.Engines.Add(new SparkViewFactory());

Finally, I recommend adding the following to web.config, although it is optional:

      <section name="spark" type="Spark.Configuration.SparkSectionHandler, Spark"/>
      <compilation debug="true" />
      <pages automaticEncoding="true" />

That’s it – Spark is now added to your app and runs alongside the default Web Forms view engine. Any views not using Spark will still work.

Converting views to Spark

In short, all you have to do to convert your views (master pages, user controls and pages) to spark is to:

  • Rename Site.master to Application.spark
  • Rename all user controls to _NAME.spark (the underline makes it possible to add user controls as HTML elements)
  • Rename all pages to NAME.spark
  • In all master page, user control and page files, remove the topmost tag
  • In Application.spark, replace all ContentPlaceHolders tags with use spark tags
  • In all pages, replace all Content tags with contentspark tags

You also have to replace:

   <%   =>   ${

For strongly types views,you can type ViewData and the Model like this:

   <viewdata Message="string" model="SparkTemplate.Models.LogOnModel" />

Note that the name of the ViewData key is defined as is while the model tag is defined in lower-case. You can then access the model like this:


Other than that, everything just seem to work.

Worth noticing is that you should use ! instead of $ for ValidationSummary and ValidationMessageFor – otherwise, empty strings will render the full Spark expression.