首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编译模板时出现RazorEngine.Templating.TemplateCompilationException:错误。ASP.NET核3

编译模板时出现RazorEngine.Templating.TemplateCompilationException:错误。ASP.NET核3
EN

Stack Overflow用户
提问于 2020-06-27 13:00:30
回答 1查看 657关注 0票数 1

使用RazorEngine将HTML错误编译为

代码语言:javascript
复制
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模板

代码语言:javascript
复制
@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>
EN

回答 1

Stack Overflow用户

发布于 2022-06-14 06:54:25

我也有过同样的问题。并通过删除字符串@model ViewModel.EmailViewModel来解决这个问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62610342

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档