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

.NET Core 2.0 and netstandard 2.0 support #515

Open denizalpaslan opened 6 years ago

denizalpaslan commented 6 years ago

Hi,

TL;DR Does this library support netstandard2.0 ? I'm trying to use RazorEngine 3.10.0

Details I have a .NET core 2.0 project (Project A) which has a reference to another project with netstandard 2.0. (Project B)

I'm trying to use RazorEngine in Project B :

Any idea?

stusklinar commented 6 years ago

Try adding the microsoft.aspnet.razor package to get System.Web.Razor

stusklinar commented 6 years ago

Also see:

498

denizalpaslan commented 6 years ago

Nope. It now gives

Exception="System.TypeLoadException" ExceptionDetail="System.TypeLoadException: Could not load type 'Microsoft.AspNetCore.Razor.Parser.ParserBase' from assembly 'Microsoft.AspNetCore.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'

which is caused by a breaking change introduced with .NET core 2.0

stusklinar commented 6 years ago

That's because razor 2.0 isn't supported, I'm also waiting on a fix.

daviatorstorm commented 6 years ago

+1

denizalpaslan commented 6 years ago

@matthid & @conniey any timeline for this update? I'm mentioning you because I see you guys as the top two contributors.

Thanks

stusklinar commented 6 years ago

Ok - Update.

There is a NetCore release for this - https://github.com/hermanho/RazorEngine/tree/updateToNetCore

This is forked from this branch, which was then forked from @conniey.

Available as a nuget package here:

https://www.nuget.org/packages/RazorEngine.Core.NetCore/

Now - whether you use this in prod - is totally up to you, I'd advise a lot of testing!

If you use a base template that doesn't have a empty ctor, this code won't work - it just won't. I've raised #517 for this.

If you use a normal base template, crack on.

mscrivo commented 6 years ago

I tried the package that @stusklinar suggested and while it compiles, it doesn't seem to work at all with my existing templates.

This dependency on Microsoft.AspNetCore.Razor 1.x is becoming really problematic because it has a dependency on System.Net.Http nuget package which completely breaks things if you target .NET 4.7.2 and also have projects that target .NET Standard and have code that uses System.Net.Http. The above package resolves those build issues, but as mentioned, does not work.

stusklinar commented 6 years ago

What are your existing templates? Can you show me an example? I May have a fix if you have an extended base template.

mscrivo commented 6 years ago

@stusklinar Here's the error I'm getting:

RazorEngine.Templating.TemplateCompilationException: 'Errors while compiling a Template.
 - error: (25, 6) The name 'inherits' does not exist in the current context

The template itself looks like this:

@using MyNameSpace
@inherits RazorEngine.Templating.TemplateBase<ModelClass>
@{
    const int maxColsPerRow = 5;
}
<tr>
A bunch of standard razor code here
</tr>

The class ModelClass is a fairly standard class, but it inherits from a base class, which implements an interface.

stusklinar commented 6 years ago

Ah ok, different error to mine.

So long as your base classes don't need ctor params, you won't run into the same issue me.

As for this, can you pass your model inherit this instead?

mscrivo commented 6 years ago

As for this, can you pass your model inherit this instead?

Not sure what you mean by that?

stusklinar commented 6 years ago

Well, as you're having a base template, in the razor config you supply to the engine, you can specify a base template class, could you supply that instead??

/// <summary> /// The type of the view base to compile templates with /// </summary> public override Type ViewBaseType { get { return typeof(ViewBase<>); } }

mscrivo commented 6 years ago

Sorry, still not sure what you're referring to? ... I don't see that ViewBaseType property anywhere. Are you referring to BaseTemplateType? The problem is, I don't have a base type for my actual templates, only the models have a common interface.

The way I'm calling the Engine is like this:

var emailHtml = Engine.Razor.RunCompile(templateName, null, emailModel);

The engine is setup like this:

var config = new TemplateServiceConfiguration
{
#if !DEBUG
    // This allows Razor Engine to clean up after itself. 
    DisableTempFileLocking = true,
#endif
    TemplateManager = new ResolvePathTemplateManager(new[] {Path.Combine(AssemblyDirectory, @"Reporting\RazorEmailTemplates")}),

    // Enable Roslyn compiler for templates to get C# 6+ features
    CompilerServiceFactory = new RoslynCompilerServiceFactory(),
#if DEBUG
    // enable debugging in templates
    Debug = true
#endif
};

var service = RazorEngineService.Create(config);

Engine.Razor = service;
stusklinar commented 6 years ago

I think they're the same thing aren't they? The base template has a model on it. And if you inherit from it, you can set your model in that?

kralikba commented 5 years ago

Hey, I'm having the same problem. Both @model and @inherits directives end up parsed as simple expressions instead of directives - i.e. the following is generated: Write(model);

mariusene commented 5 years ago

I have the same issue as @kralikba : @inherits directives are parsed as simple expressions. Any fix for this?

janniksam commented 5 years ago

Having the same issue.

I have to comment out the inherits-line when releasing my razor-views and comment it in again to design the views.

A workaround for me would be to replace the inherits line before sending it into the RazorEngine, but thats a huge hack imho.

janniksam commented 5 years ago

Temporary workaround (apply before compiling):

var regex = new Regex("^(\\s*@inherits.*)$", RegexOptions.Multiline);
parsableTemplate = regex.Replace(parsableTemplate, string.Empty);
one-matrix commented 4 years ago

hope support netcore