5

How do you modify a ASP.NET MVC 2.0 project to work with the Spark View Engine?

I tried like described here: http://dotnetslackers.com/articles/aspnet/installing-the-spark-view-engine-into-asp-net-mvc-2-preview-2.aspx

But somehow it still tries to route to .aspx files.

Here the code of my global.asax:

public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );

    }

    protected void Application_Start()
    {
        SparkViewFactory svf = new SparkViewFactory();
        PrecompileViews(svf);

        AreaRegistration.RegisterAllAreas();

        RegisterRoutes(RouteTable.Routes);
    }

    public static void PrecompileViews(SparkViewFactory svf)
    {
        var controllerFactory = svf;
        var viewFactory = new SparkViewFactory(controllerFactory.Settings);
        var batch = new SparkBatchDescriptor();
        batch
            .For<HomeController>()
            .For<AccountController>();
        viewFactory.Precompile(batch);
    }
}

}

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
Ben
  • 2,560
  • 4
  • 29
  • 43
  • look like nobody has solution for this issue, lets wait release :( – Sasha Jan 20 '10 at 07:21
  • This is temporarily broken in the most recent releases of ASP.NET MVC 2. See http://stackoverflow.com/questions/2138583/cant-set-up-asp-net-mvc-2-rc-and-spark-view-engine for a workaround you can use in the meantime. – Eilon Feb 01 '10 at 04:58

5 Answers5

3

http://www.simple-talk.com/community/blogs/asiemer/archive/2010/01/31/89132.aspx

I had to download the spark view engine source code (http://sparkviewengine.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=27600). Once I did that I went through each of the projects that had a reference to the 1.0 version of System.Web.Mvc assembly and updated to reference to point to System.Web.Mvc 2.0. From there you can build the solution (in visual studio) and you will find that a whole bunch of tests start to fail. You can attempt to fix them (by adding the additional TextWriter parameter you will find is now needed). You will also see that the SparkView.cs file complains about a missing parameter. In the Render method (line 100 of the source code I downloaded) I had to update the instantiation of the wrappedViewContext to look like this (add writer to the end of the list of parameters):

public void Render(ViewContext viewContext, TextWriter writer)
{
    var wrappedHttpContext = new HttpContextWrapper(viewContext.HttpContext, this);

    var wrappedViewContext = new ViewContext(
        new ControllerContext(wrappedHttpContext, viewContext.RouteData, viewContext.Controller),
        viewContext.View,
        viewContext.ViewData,
        viewContext.TempData,
        writer); //  <-- add the writer to the end of the list of parameters

    ...
}

Once the code is updated you can run the build.cmd script that is in the root of the source you downloaded. The build process will create a zip file in the build/dist folder. Take those new dll's and add them to your website. Things should work once again.

Andrew Siemer
  • 10,166
  • 3
  • 41
  • 61
2

You need to register the Viewengine:

ViewEngines.Engines.Add(new SparkViewFactory());
  • Hmm i already tried. It resulted in: Method not found: 'Void System.Web.Mvc.ViewContext..ctor(System.Web.Mvc.ControllerContext, System.Web.Mvc.IView, System.Web.Mvc.ViewDataDictionary, System.Web.Mvc.TempDataDictionary)'. – Ben Jan 14 '10 at 15:31
  • @ben this is broken only temporarily. The author of SparkViewEngine is working on a fix but he's a bit busy these days. – Eilon Feb 01 '10 at 04:57
1

If you fancy rolling your own then there is a fix on the sparkview google group.

Personally I'd wait for the next release.

Ryan Barrett
  • 1,007
  • 10
  • 14
0

My global.asax.cs contains this:

public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );

    }

    protected void Application_Start()
    {
        RegisterRoutes(RouteTable.Routes);
        ViewEngines.Engines.Add(new SparkViewFactory());

    }
}

and my web.config contains this:

<configSections>
    <section name="spark" type="Spark.Configuration.SparkSectionHandler, Spark"/>
    <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
        <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
            <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
            <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
                <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
                <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
            </sectionGroup>
        </sectionGroup>
    </sectionGroup>
</configSections>

<spark>

    <pages>
        <namespaces>
            <add namespace="System"/>
            <add namespace="System.Collections.Generic"/>
            <add namespace="System.Linq"/>
            <add namespace="System.Web.Mvc"/>
        </namespaces>
    </pages>
</spark>
D'Arcy Rittich
  • 167,292
  • 40
  • 290
  • 283
0

I would look at the samples comes with the Spark-1.0.zip package. Looking at one of them randomly has this in the Global.asax.cs

SparkEngineStarter.RegisterViewEngine();

Hope it helps.

mtmk
  • 6,176
  • 27
  • 32