Tuesday, December 22, 2009

A Global.Asax example to use the Spark View Engine in Microsoft ASP.Net MVC

To help those getting started using Microsoft ASP.Net MVC, I have copied my exact Global.asax file below. 

Note that I am using the Spark View Engine and clear the default view engine before adding Spark. Telling someone how to change global.asax file to use Spark is fine, but I think this "here it is" example will be very helpful to many starting to use the Spark View Engine.

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

namespace MySystem
{
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute("mvcroute", "{controller}/{action}/{id}"
, new { controller="MyHome",
action = "Index", id = "" }
, new { controller=@"[^\.]*"});
}
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
ViewEngines.Engines.Clear();
SparkEngineStarter.RegisterViewEngine();
}

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("~/MyHome");
}
}
}
}


Hope that helps.


Joe Kunk

Microsoft MVP


Okemos, MI USA


12-22-2009


 

No comments: