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

6

9

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" />
Deli answered 26/11, 2009 at 21:38 Comment(1)
I think the real problem here was that I had mistakenly thought that the version of MVCContrib I was using was using the 2.0.0 version of system.web.mvc.dll - it wasn't! Crap! After updating that, spark, and ninject - it works!Deli
D
9

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.

Deli answered 5/12, 2009 at 22:10 Comment(1)
Thanks Ian, I have the same problem and fixed by following your methods. There was an assembly that references system.web.mvc.dll v1.0.Biplane
R
2

I recently converted my spark project to asp.net mvc 2. I dont see in what you've written that you've switched from using Spark.Web.Mvc to Spark.Web.Mvc2.

Just to be clear, Spark.Web.Mvc2 is the project that needs to be recompiled with the new System.Web.Mvc assembly.

I had issues with intellisense until i removed all assembly and namespace references from the web.config and put them in the global.spark file and had added the pageBaseType="Spark.Web.Mvc2.SparkView" attribute to the Spark->Pages node in the web.config.

Hope any of that helps.

Rosenfeld answered 2/12, 2009 at 18:21 Comment(1)
Thanks for the info, I double checked that the latest version of Spark does, in-fact, include Spark.Web.Mvc2.dll which references system.web.mvc.dll v 2.0.0. I was able to use this successfully in my project, however I still mistakenly had overlooked another reference to v1.0.0 elsewhere!Deli
T
1

When you downloaded newest Spark sources, did you rebuild it using MVC 2 assemblies? Did you replace references in Spark project?

Teutonism answered 28/11, 2009 at 19:27 Comment(7)
Yes - I've re-worded the sentence in my question that said "upgraded to the latest version of Spark" to "Upgraded Spark to use MVC 2" to clarify what I meant there. I have recompiled the Spark source with MVC 2 references and incorporated the resultant assemblies into my MVC project and updated its references. No change in error message. I've also looked at the assemblies being used in my project in Reflector to verify that they are referencing System.Web.Mvc version 2.0.0.0.Deli
Did you pass model to view? did you define "<viewdata model=''>"?Teutonism
Here is the full content of my view: <viewdata model="Entity" /> <use namespace="System.Web.Mvc.Html"/> <h2>Entity</h2> <h3>Name</h3> <h4>${Model.Name}</h4> ${Html.EditorFor(e => e)} <a href="!{Url.Action<EntityController>(c => c.Index(null))}">Back</a>Deli
and to clarify - I have a view.aspx and a view.spark - to test one or the other I just rename the one I don't want to use to something else. ASPX works, Spark does not - so this would seem to be a valid test to ensure that everything involved is working properly until I use the spark view instead of the aspx view. Thanks for your help and thoughts, btw. It's much appreciated - I've been stumped on this for awhile now.Deli
I am having trouble building new version of Spark. What type is HtmlHelper in Spark view? Is it HtmlHelper ot HtmlHelper<TModel>?Teutonism
Thanks again for spending time on this. As it turns out, when you download and use the latest version of Spark, there is an assembly compiled against system.web.mvc.dll v2.0.0 that is ready to use.Deli
I rebuilt latest spark sources and replaced the reference by spark.web.mvc2 but I now get weird syntax errors such as: Dynamic view compilation failed. error CS1002: ; expected . Anybody has an idea on how to solve this? (spark latest sources as of today:1.0.39966.0)Crus
G
1

Try specifiying the System.Web.Mvc reference in the web.config instead of the Global.ascx.cs file so that you can specify the specific version:

<spark>
  <compilation debug="true" defaultLanguage="CSharp">
     <assemblies>
        <add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
     </assemblies>
 </compilation>
</spark>

This should prevent SPARK from working at all if the wrong version of the MVC assembly is being loaded from somewhere.

If the MVC 1.0 Assembly is in the GAC you can clear and reload it as follows:

(from Visual Studio command prompt)

ngen /delete System.Web.Mvc
ngen /delete System.Web.Abstractions
ngen update

Hope this helps,

Jeff French

Genisia answered 1/12, 2009 at 18:52 Comment(3)
I have the following in the system.web > assemblies section of my web.config - is that sufficient? <add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> If not, can you elaborate on how/why to add the <spark> section to the web.config?Deli
Adding the <spark> section to the web.config is an alternate way to specify the settings. It is equivilent to what you have in your Global.asax file (ie: var settings in new SparkSettings()...). The reason I suggested moving your settings to the web.config <spark> section is that you can specify the strong assembly name and type there instead of just telling spark to look for System.Web.Mvc. Also note that this is an Assembly reference in spark in addition to the Namespace references you have already specified in the Global.asax file.Genisia
Thanks for the clarification Jeff. This was very helpful, however it didn't directly solve my issue (I had to search and destroy all references to the old version of the system.web.mvc assembly for real). And you're right, if you follow this step it kills spark everywhere if the 1.0 version of system.web.mvc.dll is referenced.Deli
H
1

These are the steps I took to resolve the issue with a new MVC 2 project and Spark 1.1:

  1. Compile against MVC 2.0 - I double checked the references to make sure I was linking to MVC 2 and not MVC 1. Since this was a new project, this was not an issue.

  2. Added System.Web.Mvc.Html - I added System.Web.Mvc.Html to the Spark configuration, to make sure that namespace was added to all views.

    In Global.asax.cs Application_Start

     var settings = new SparkSettings()
         .SetDebug(true)
         .SetAutomaticEncoding(true)
         .AddAssembly("Web")
         .AddNamespace("Web.Model")
         .AddNamespace("System.Collections.Generic")
         .AddNamespace("System.Linq")
         .AddNamespace("System.Web.Mvc")
         .AddNamespace("System.Web.Mvc.Html");
    

    This can also be done in the webconfig in the Spark View Engine block.

  3. Add the Typed Model - Make sure you type the Spark View Model. In aspx this is done with the Inherits in the page declaration, like this:

    <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
    Inherits="System.Web.Mvc.ViewPage<MyModelType>" %>
    

    in Spark:

    <viewdata model="MyModelType" />
    
Hadsall answered 6/12, 2010 at 18:40 Comment(0)
S
0

I tried a lot of things mentioned above but just couldn't get a dependent dll that my views used (and also referenced MVC 1.0.0.0) to use the MVC 2.0.0.0, so here is what fixed it for me...

I downloaded the Spark code and added the following line to the BatchCompiler.cs file in the Compile method:

compilerParameters.CompilerOptions = "/nowarn:1701";
Septimal answered 23/9, 2010 at 18:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.