just ram

stuff I should remember

Setup ASP.NET MVC with Spark View Engine in 5 minutes or less

Setup folder structure:

image

Download and put the asp.net mvc dll’s in mvc folder and spark dll’s in spark folder.

Create a new asp.net application not a mvc application just a standard asp.net application. This means you will not get the mvc helpers for creating controllers and views but that’s not a massive loss and has the advantage that you don’t need the asp.net mvc installed if you open the solution on another machine.

add new projectChange content of Default.aspx to:

The default.aspx placeholder is required to allow the asp.net routing to work.

Delete the code behind files for Default.aspx (Default.aspx.cs, Default.aspx.designer.cs) they are not needed.

Add references to the Spark.dll and Spark.Web.Mvc.dll located in the libs folder.

vs_add_ref

Add references to asp.net mvc System.Web.Routing.dll, System.Web.Abstractions.dll, System.Web.Mvc.dll

image

Add spark to the web.config:

</sectionGroup>
<section name=”spark” type=”Spark.Configuration.SparkSectionHandler, Spark”/>
</configSections>

Debug true shows compilation errors when rendering views.

Add routing to httpModules section of web.config:

Add a application.cs which creates the routes and registers the spark view engine:

using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Routing;
using Spark.Web.Mvc;

namespace Web
{
public class Application
{
public void RegisterRoutes(IList routes) { routes.Add(new Route( “{controller}/{action}/{id}”, new RouteValueDictionary(new {controller = “home”, action = “index”, id = “”}), new MvcRouteHandler())); }

public void RegisterViewEngine(IList engines) { SparkEngineStarter.RegisterViewEngine(engines); } } }

Add a global.asax with the following code behind.

image

using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace Web
{
public class Global : HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
var app = new Application();
app.RegisterRoutes(RouteTable.Routes);
app.RegisterViewEngine(ViewEngines.Engines);
}

protected void Application_BeginRequest(object sender, EventArgs e)
{
string path = Request.AppRelativeCurrentExecutionFilePath;
if (string.Equals(path, “~/default.aspx”, StringComparison.InvariantCultureIgnoreCase) ||
string.Equals(path, “~/”))
{
Context.RewritePath(“~/Home”);
}
}
}
}

Create content, controllers and view folders in the root of the asp.net project:

image

Create a shared folder in the views folder with a _global.spark file this will be used to share come code with all views.

Create a layouts folder in the views folder with a file called application.spark which will be used for the layout of the application very much like a master page.

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>

Sample

Header

** **

Footer

</html>

The use content view tag highlighted above is replaced by the current view returned by asp.net mvc.

Add a HomeController.cs under the controllers folder with the following content:

using System.Web.Mvc;

namespace Web.controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
}

Under the views folder create a home\index.spark with the following content:

Hello spark.

Run the project and you should get:

image

You can download this sample solution here.

Comments