Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.NET Core 2.0 and netstandard 2.0 support #515

Open
denizalpaslan opened this issue Mar 8, 2018 · 19 comments

Comments

@denizalpaslan
Copy link

commented Mar 8, 2018

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 :

  • I added package with "dotnet add package RazorEngine". It succeeded with 3.10.0 with a warning that it may not be fully compatible with my project.

  • I wrote the code and it builds successfully. When I try to run it I got "Could not load file or assembly 'System.Configuration.ConfigurationManager ...." error and I added this package reference explicitly (saw another issue about that before)

  • The second time I ran the code, I got "Could not load file or assembly 'System.Web.Razor ..." which doesn't seem to be part of netstandard2.0

Any idea?

@stusklinar

This comment has been minimized.

Copy link

commented Mar 8, 2018

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

@stusklinar

This comment has been minimized.

Copy link

commented Mar 8, 2018

Also see:

#498

@denizalpaslan

This comment has been minimized.

Copy link
Author

commented Mar 8, 2018

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

This comment has been minimized.

Copy link

commented Mar 8, 2018

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

@daviatorstorm

This comment has been minimized.

Copy link

commented Mar 8, 2018

+1

@denizalpaslan

This comment has been minimized.

Copy link
Author

commented Mar 11, 2018

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

Thanks

@stusklinar

This comment has been minimized.

Copy link

commented Mar 15, 2018

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

This comment has been minimized.

Copy link

commented May 2, 2018

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

This comment has been minimized.

Copy link

commented May 2, 2018

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

@mscrivo

This comment has been minimized.

Copy link

commented May 2, 2018

@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

This comment has been minimized.

Copy link

commented May 2, 2018

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

This comment has been minimized.

Copy link

commented May 2, 2018

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

Not sure what you mean by that?

@stusklinar

This comment has been minimized.

Copy link

commented May 2, 2018

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

This comment has been minimized.

Copy link

commented May 2, 2018

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

This comment has been minimized.

Copy link

commented May 2, 2018

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

This comment has been minimized.

Copy link

commented Mar 7, 2019

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

This comment has been minimized.

Copy link

commented Mar 28, 2019

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

@janniksam

This comment has been minimized.

Copy link

commented Mar 29, 2019

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

This comment has been minimized.

Copy link

commented Mar 31, 2019

Temporary workaround (apply before compiling):

var regex = new Regex("^(\\s*@inherits.*)$", RegexOptions.Multiline);
parsableTemplate = regex.Replace(parsableTemplate, string.Empty);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
7 participants
You can’t perform that action at this time.