Antaris / RazorEngine

Open source templating engine based on Microsoft's Razor parsing engine
http://antaris.github.io/RazorEngine
Other
2.14k stars 577 forks source link

Compatible with Razor 2.0? #42

Closed JonEHolland closed 11 years ago

JonEHolland commented 12 years ago

Trying to get RazorEngine to work again after upgrading a project from System.Web.Razor 1.0.0 to System.Web.Razor 2.0.0 (In MVC 4 Beta).

At first I was getting:

[FileLoadException: Could not load file or assembly 'System.Web.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)] RazorEngine.Compilation.DefaultCompilerServiceFactory.CreateCompilerService(Language language) +0 RazorEngine.Templating.TemplateService.CreateTemplateType(String razorTemplate, Type modelType) +1143 RazorEngine.Templating.TemplateService.CreateTemplate(String razorTemplate, T model) +166 RazorEngine.Templating.TemplateService.Parse(String razorTemplate, T model) +187 RazorEngine.Razor.Parse(String razorTemplate, T model) +184

I added a binding redirect to the app:

  <dependentAssembly>
    <assemblyIdentity name="System.Web.Razor" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-1.0.0.0" newVersion="2.0.0.0" />
  </dependentAssembly>

Now, I get:

[MissingMethodException: Method not found: 'System.Collections.Generic.Dictionary2<System.String,BlockParser> System.Web.Razor.Parser.CSharpCodeParser.get_RazorKeywords()'.] RazorEngine.Compilation.CSharp.CSharpCodeParser..ctor() +0 RazorEngine.Compilation.RazorEngineHost.DecorateCodeParser(ParserBase incomingCodeParser) +138 System.Web.Razor.RazorTemplateEngine.CreateParser() +57 System.Web.Razor.RazorTemplateEngine.GenerateCodeCore(ITextDocument input, String className, String rootNamespace, String sourceFileName, Nullable1 cancelToken) +79 System.Web.Razor.RazorTemplateEngine.GenerateCode(TextReader input, String className, String rootNamespace, String sourceFileName, Nullable1 cancelToken) +126 System.Web.Razor.RazorTemplateEngine.GenerateCode(TextReader input) +85 RazorEngine.Compilation.CompilerServiceBase.GetGeneratorResult(RazorEngineHost host, String template) +187 RazorEngine.Compilation.CompilerServiceBase.GetCodeCompileUnit(String className, String template, ISet1 namespaceImports, Type templateType, Type modelType) +982 RazorEngine.Compilation.DirectCompilerServiceBase.Compile(TypeContext context) +442 RazorEngine.Compilation.DirectCompilerServiceBase.CompileType(TypeContext context) +189 RazorEngine.Templating.TemplateService.CreateTemplateType(String razorTemplate, Type modelType) +1526 RazorEngine.Templating.TemplateService.CreateTemplate(String razorTemplate, T model) +166 RazorEngine.Templating.TemplateService.Parse(String razorTemplate, T model) +187 RazorEngine.Razor.Parse(String razorTemplate, T model) +184

This is with the latest RazorEngine nuget package.

Is RazorEngine not compatible with System.Web.Razor 2.0.0?

petejstephens commented 12 years ago

I have the same issue, I've just migrated my MVC4 which uses system.web.razor 2.0.0.0 to use Postal, which uses RazorEngine. Unfortunately after I deploy my project to its Azure environment I get:

"Could not load type 'System.Web.Razor.Parser.SyntaxTree.CodeSpan' from assembly 'System.Web.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'".

I'm also using the same bindingRedirect.

So I'm a bit stuck at the moment and looking to rollback to MVC3.

Any ideas?

ghost commented 12 years ago

I've the same issue

georgiosd commented 12 years ago

They removed some classes and refactored others in v2 - which breaks RazorEngine :(

pjanuario commented 12 years ago

I'm having the same problem. Is there any workaround for it?

georgiosd commented 12 years ago

The only way I could find was to remove the redirect to WebPages 2.0 from web.config placed there by NuGet - so far no problems but I'm not using any other packages that require this assembly

pjanuario commented 12 years ago

Where is that redirect you are talking about? And for that i need to include web pages 2.0 and 1.0 assemblies on the solution?

georgiosd commented 12 years ago

Yes. Be careful because nuget seems to re-add it if you change packages

Somehow the XML is not showing up - in web.config there's a bindingRedirect for 0-2 of System.Web.Razor to 2.0

georgiosd commented 12 years ago

PS: its System.Web.Razor, I made a mistake earlier

Aaronontheweb commented 12 years ago

So are there still no work-arounds or fixes for this issue yet?

georgiosd commented 12 years ago

I found one! http://stackoverflow.com/a/10936596/165656

Aaronontheweb commented 12 years ago

@georgiosd does it look like the app.config step is missing from that submission?

georgiosd commented 12 years ago

The XML was in the answer but wasn't showing... fixed that :)

jonnii commented 12 years ago

I think this problem is becoming more and more pressing, especially with the release of asp.net mvc 4. I've looked into what it'd take to upgrade to the latest version of razor, but I don't have a deep enough understanding of the compiler internals.

I've got most of the unit tests passing, with the exception of the ones that explicitly declare the @model. It looks like this isn't a built in feature for razor and the existing code uses a keyword extension. The problem is that API has completely changed...

I looked through the asp.net mvc code to see how they do it, but I can't find where they're doing it... Any thoughts?

georgiosd commented 12 years ago

Have you tried posting at the http://aspnetwebstack.codeplex.com/ forums? The MS folks are usually quite good at engaging with the community.

jonathanxu commented 12 years ago

Time to upgrade RazorEngine to use System.Web.Razor 2.0?

Antaris commented 12 years ago

There is some work to do, as System.Web.Razor v1 and v2 aren't API-compatible, there are some under the hood changes that would break the current version of RazorEngine. I'm investigating either a switch to v2, or looking to provide two versions, one for v1 and v2 compatability.

paultyng commented 12 years ago

Any way to help? Is there a branch of a v2 compatible version of RazorEngine we could use?

georgiosd commented 12 years ago

See my SO post above for a temp fix.

coxp commented 12 years ago

I have an experimental fork of the project that I'm currently using that I've upgraded to v2.

markchivs commented 11 years ago

Hi coxp is your v2 compatible version available at all? Thanks Mark

markchivs commented 11 years ago

just saw this: https://github.com/Antaris/RazorEngine/issues/71

coxp commented 11 years ago

Hi,

You can build the dll using my fork but there isn't a NuGet package available. I'm using this in production but YMMV.

https://github.com/coxp/RazorEngine

This is using the Razor 2 dll so you get support for the new features like null attributes.

There are no tests though that's why I haven't made a pull request.

Antaris commented 11 years ago

v3.1.0 introduced Razor 2 compatability, available on Nuget.