我想使用RazorEngine创建电子邮件,但是当我的应用程序在服务器上工作时返回错误
堆栈跟踪: RazorEngine.Compilation.CrossAppDomainCleanUp.CleanupHelper.Init(的AppDomain 域,IPrinter打印机) RazorEngine.Compilation.CrossAppDomainCleanUp.InitHelper.CreateHelper() 在 RazorEngine.Compilation.ExecutionContextLessThread.FuncConv1.Call(布尔 数据) RazorEngine.Compilation.ExecutionContextLessThread.CallHelperSafeHelper2.AsAction() 在 RazorEngine.Compilation.ExecutionContextLessThread.CallHelperSafeHelper2.AsContextCallback(对象 国家) System.Threading.ExecutionContext.RunInternal(执行上下文 executionContext,ContextCallback回调,对象状态,布尔值 preserveSyncCtx)at System.Threading.ExecutionContext.Run(执行上下文 executionContext,ContextCallback回调,对象状态,布尔值 preserveSyncCtx)at System.Threading.ExecutionContext.Run(执行上下文 executionContext,ContextCallback回调,对象状态)at RazorEngine.Compilation.ExecutionContextLessThread.DefaultCallFunc [O](FUNC1 f)在RazorEngine.Compilation.CrossAppDomainCleanUp..ctor(AppDomain toWatch,IPrinter打印机)at RazorEngine.Compilation.CrossAppDomainCleanUp.CreateInitial()at System.Lazy1.CreateValue()---来自之前的堆栈跟踪结束 抛出异常的位置--- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()at System.Lazy1.getValue()at RazorEngine.Compilation.CrossAppDomainCleanUp.getCurrentCleanup()at RazorEngine.Compilation.CrossAppDomainCleanUp.RegisterCleanup(字符串 item,Boolean throwOnDefault)at RazorEngine.Templating.InvalidatingCachingProvider<>℃下。.ctor> B50(字符串 项目) RazorEngine.Templating.InvalidatingCachingProvider.CacheTemplate(ICompiledTemplate 模板,ITemplateKey templateKey)at RazorEngine.Templating.DefaultCachingProvider.CacheTemplate(ICompiledTemplate 模板,ITemplateKey templateKey)at RazorEngine.Templating.RazorEngineService.CompileAndCacheInternal(ITemplateKey key,Type modelType)at RazorEngine.Templating.RazorEngineService.GetCompiledTemplate(ITemplateKey key,Type modelType,Boolean compileOnCacheMiss)at RazorEngine.Templating.RazorEngineService.RunCompile(ITemplateKey key, TextWriter writer,Type modelType,Object model,DynamicViewBag viewBag)在 RazorEngine.Templating.DynamicWrapperService.RunCompile(ITemplateKey key,TextWriter writer,Type modelType,Object model,DynamicViewBag viewBag)在 RazorEngine.Templating.RazorEngineServiceExtensions<> cDisplayClass160.b__0(TextWriter的 作家) RazorEngine.Templating.RazorEngineServiceExtensions.WithWriter(措施1 withWriter)at RazorEngine.Templating.RazorEngineServiceExtensions.RunCompile(IRazorEngineService service,String name,Type modelType,Object model,DynamicViewBag viewBag)在project.Email.CreateEmail(List1提供) 消息:对象类型无法转换为目标。
这是我的代码,哪里是错误。
private string CreateEmail(List<EmailOffer> offers)
{
string template=File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "Views\\EmailTemplate\\Email.cshtml"));
return Engine.Razor.RunCompile(template, "templateKey", null, offers);
}
这是类EmailOffer
public class EmailOffer
{
public string Title{ get; set; }
public string UrlImage { get; set; }
}
这是模板电子邮件(文件cshtml)
@model IEnumerable<project.Models.EmailOffer>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="viewport" content="width=device-width" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
@foreach (var item in Model)
{
<table style="margin-bottom: 10px;margin-top: 25px;background-color: #f3f3f3;padding: 10px;">
<tr>
<td style="max-width: 128px;padding-right: 10px;height: auto;min-width: 64px;width: 20%;"><img src="@item.UrlImage" style="max-width: 100%;" /></td>
<td>
<h2 style="line-height: 1.1;margin: 10px;font-weight: 500;font-size: 16px;">@item.Title</h2>
</td>
</tr>
</table>
}
</body>
</html>
我该如何解决?非常感谢你。
答案 0 :(得分:0)
您是否需要指定要传递给RunCompile方法的模型类型?
e.g。从这个文件: https://antaris.github.io/RazorEngine/TemplateBasics.html
word_1 topic_b:freq_of_word_1 topic_a:freq_of_word_1 ....
word_2 topic_a:freq_of_word_2 topic_d:freq_of_word_2 ....
.
.
此外,您可以尝试完全限定模板文件中集合中包含的类型,例如
private string CreateEmail(List<EmailOffer> offers)
{
string template=File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "Views\\EmailTemplate\\Email.cshtml"));
return Engine.Razor.RunCompile(template, "templateKey", typeof(List<EmailOffer>), offers);
}