使用RazorEngine将HTML错误编译为
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模板
@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>发布于 2022-06-14 06:54:25
我也有过同样的问题。并通过删除字符串@model ViewModel.EmailViewModel来解决这个问题。
https://stackoverflow.com/questions/62610342
复制相似问题