Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RazorEngine.Templating.TemplateCompilationException: Errors while compiling a Template. ASP.NET core 3

Using RazorEngine to compile the HTML getting error as

RazorEngine.Templating.TemplateCompilationException: Errors while compiling a Template.
Please try the following to solve the situation:
  * If the problem is about missing/invalid references or multiple defines either try to load 
    the missing references manually (in the compiling appdomain!) or
    Specify your references manually by providing your own IReferenceResolver implementation.
    See https://antaris.github.io/RazorEngine/ReferenceResolver.html for details.
    Currently all references have to be available as files!
  * If you get 'class' does not contain a definition for 'member': 
        try another modelType (for example 'null' to make the model dynamic).
        NOTE: You CANNOT use typeof(dynamic) to make the model dynamic!
    Or try to use static instead of anonymous/dynamic types.



private string RunCompile(string rootPath, string templateName, EmailViewModel model, string templateKey = null)
        {
            string result = string.Empty;

            if (string.IsNullOrEmpty(rootPath) || string.IsNullOrEmpty(templateName) || model == null) return result;

            string templateFilePath = Path.Combine(rootPath, templateName);

            if (File.Exists(templateFilePath))
            {
                string template = File.ReadAllText(templateFilePath);

                if (string.IsNullOrEmpty(templateKey))
                {
                    templateKey = Guid.NewGuid().ToString();
                }

                result = Engine.Razor.RunCompile(template, templateKey, typeof(EmailViewModel), model);
            }

            return result;
        }

HTML template

@model ViewModel.EmailViewModel
<table bgcolor="white" align="left" border="0" cellspacing="0" cellpadding="0" style="color:#5E5E5E;font-size:14px;font-family:Proxima Nova,Century Gothic,Arial,Verdana,sans-serif;width:100%;border:1px solid #F0F0F0; padding: 32px;">
    <tbody>
    <tr align="center">
        <td style="padding-top:30px;padding-bottom:32px;">
            <img src="https://ok6static.oktacdn.com/bc/image/fileStoreRecord?id=fs01fviisxo2dNOCM2p7" alt="monash university logo"/>
        </td>
    </tr>
    <tr>
        <td style="padding-top:24px;">
            <div style="font-family:Consolas,Courier New,Courier,monospace;text-align:left;margin:10px;">
                <h1>
                    Thank you for the registration
                </h1>
            </div>
        </td>
    </tr>
    <tr>
        <td style="padding-top:24px;">
            <div style="font-family:Consolas,Courier New,Courier,monospace;text-align:left;margin:10px;">
                <p>Please click on the below link to confirm email address</p>
            </div>
        </td>
    </tr>
    <tr>
        <td style="padding-top:24px;">
            <div style="font-family:Consolas,Courier New,Courier,monospace;text-align:left;margin:10px;">
                @Model.Message
            </div>
        </td>
    </tr>
    </tbody>
</table>
like image 460
San Jaisy Avatar asked Jul 05 '26 13:07

San Jaisy


1 Answers

I had the same issue. And resolved it with removing string @model ViewModel.EmailViewModel

like image 126
Vladyslav Fomin Avatar answered Jul 07 '26 08:07

Vladyslav Fomin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!