adoconnection / RazorEngineCore

.NET6 Razor Template Engine
MIT License
565 stars 84 forks source link

System.TypeLoadException. #112

Closed Federico-Luna closed 1 year ago

Federico-Luna commented 2 years ago

Following the basic usage, I'm trying to compile a razor page to generate the html for an order and send it via email, but I get this error when I try to compile it:

Message=Method 'CreateDeterministicKeyBuilder' in type 'Microsoft.CodeAnalysis.CSharp.CSharpCompilationOptions' from assembly 'Microsoft.CodeAnalysis.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' does not have an implementation. Source=RazorEngineCore

The api: `

    [HttpGet("GenerahtmlPedido/{idPedido}")]
    public ActionResult<string> GenerahtmlPedido(int idPedido)
    {
        DirectoryInfo dinfo = new DirectoryInfo(Path.Combine(_environment.ContentRootPath, "pages"));
        var cshtmlFile = Path.Combine(dinfo.FullName, "Pedido.cshtml");

        string strPageContent, strHtml;

        using (StreamReader r = new StreamReader(cshtmlFile))
        {
            strPageContent = r.ReadToEnd();

            IRazorEngine razorEngine = new RazorEngine();
            IRazorEngineCompiledTemplate template = razorEngine.Compile(strPageContent);

            strHtml = template.Run(new { id = idPedido });
        }

        return strHtml;
    }

` Thank you very much.

Attached razor page to compile and models. RazorPage&Models.zip

adoconnection commented 2 years ago

Hi, your page "Pedido" has a lot of extra stuff in it. for example @htmllocalizer Start with something simple. Pass PedidoModel and try render something like <p>@Model.Pedido.Id</p> then add other things so you will instantly see what caused a problem 👍

Federico-Luna commented 2 years ago

@htmlLocalizer is for localization, spain spanish, Peru spanish, USA English, British English, etc. and is mandatory, but I'll try to put literals instead of @htmllocalizer and try step by step.

Thank you again.

Federico-Luna commented 2 years ago

The new Pedido. I get same error. NewSimplePedido.zip

Pedido = Order.

Is it possible with RazorEngineCore to run a precompiled page? For example, because my main project has several dependencies with other projects like location services, I want to try to create a new project (PedidosTemplate (OrdersTemplate) for example) with the Pedido page and its dependencies included, then in my main project create a project dependency to PedidosTemplate and run OrderTemplate.dll sending the Id of the order I want to generate.

Thank you again.

adoconnection commented 2 years ago

There is a very important concept to keep in mind: RazorEngineCore is not part of MVC, you can not take existing views and make RazorEngine make compile it to string. Think of it as a stand alone product.

I think you need to get rid of

@page
@model Tienda.Pages.PedidoModel
@{
    Layout = null;
}
Federico-Luna commented 2 years ago

Ok. Thank you very much for your help.