Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using asp.net mvc 2 features with the spark view engine

I am working with an ASP.NET MVC project which was originally started from the CodeBetter.Canvas project - and I'm trying to move to ASP.NET MVC 2. I successfully upgraded my project using Eilon's upgrade tool, moved to VS2010 (although not yet to .NET 4).

The issue I'm having currently is only occurring when using the spark view engine. Here is the relevant bit of code in my View.spark (strongly typed):

${Html.EditorFor(e => e)}

The same bit of code works just fine if I use an .aspx view:

<%= Html.EditorFor(e => e) %>

The major point here being "EditorFor" is new in ASP.NET MVC 2 and in my project I can use that helper in an ASPX view but not a Spark view.

I've tried upgrading Spark to use MVC 2 (as well as MvcContrib and Ninject), thinking maybe it was one of those that was freaking out - but so far no luck - I'm still seeing the same behavior.

Here is the full error message that is thrown from within Spark's BatchCompiler class.

Dynamic view compilation failed. (0,0): warning CS1701: Assuming assembly reference 'System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' matches 'System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35', you may need to supply runtime policy c:\inetpub\wwwroot[myproject]\CodeBetter.Canvas.Web\Views[MyEntity]\View.spark(9,16): error CS1061: 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'EditorFor' and no extension method 'EditorFor' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)

Here is the Spark related code in my Global.asax:

var settings = new SparkSettings()
.AddNamespace("System")
.AddNamespace("System.Collections.Generic")
.AddNamespace("System.Web.Mvc")
.AddNamespace("System.Web.Mvc.Html")
.AddNamespace("MvcContrib.FluentHtml")
.AddNamespace("CodeBetter.Canvas")
.AddNamespace("CodeBetter.Canvas.Web")
.SetPageBaseType("ApplicationViewPage")
.SetAutomaticEncoding(true);

#if DEBUG
    settings.SetDebug(true);
#endif

var viewFactory = new SparkViewFactory(settings);
ViewEngines.Engines.Add(viewFactory);

Also, I am referencing System.Web.Mvc.Html in my spark view as mentioned in another SO answer.

<use namespace="System.Web.Mvc.Html" />
like image 533
Ian Robinson Avatar asked Nov 26 '09 21:11

Ian Robinson


1 Answers

The underlying issue is that a version 1.0.0 assembly is still being referenced somewhere in the project.

Using reflector to examine each assemblies dependencies, I found (if you're starting with CodeBetter.Canvas project) there are three dependent projects that need to be updated to use the 2.0.0 version of system.web.mvc.dll

  1. Spark. Download the latest version of spark from teamcity and use the spark.web.mvc2.dll (which references system.web.mvc.dll 2.0.0) Link to detailed instructions.

  2. MVCContrib. Download the latest version of MVCContrib (download release, download source) which references the system.web.mvc.dll 2.0.0.

  3. Ninject. Download the latest version of ninject and recompile the VS solution after updating the reference to the 2.0.0 version of system.web.mvc.dll

Now, replace these dependency's assemblies in your project and update the project references if necessary. All should be well.

like image 77
Ian Robinson Avatar answered Oct 20 '22 21:10

Ian Robinson