我有一个控制台应用程序,它使用RazorEngine根据应用程序的运行发送电子邮件。我能够成功地在开发IDE和通过可执行文件运行控制台应用程序发送电子邮件。但是,当我从计划的任务运行控制台应用程序时,我在网上收到一个错误“无法解析模板”,
var emailHtmlBody = Engine.Razor.RunCompile("ResponseErrors.cshtml", null, model);有人碰到过这个吗?
var config = new TemplateServiceConfiguration
{
TemplateManager = new ResolvePathTemplateManager(new[] {"EmailTemplates"}),
DisableTempFileLocking = true
};
Engine.Razor = RazorEngineService.Create(config);
var emailHtmlBody = Engine.Razor.RunCompile("ResponseErrors.cshtml", null, model);更新1:
更新2:
没有RazorEngine..。在用户的临时文件夹中创建的文件夹。
更新3:
我刚刚创建了一个示例控制台应用程序,它现在正在IDE中做同样的事情。我把它放在测试上
更新4:
似乎TemplateServiceConfiguration没有按照设计的方式工作,或者我编写的示例不正确。我用下面的2行更新了测试项目,这解决了我的问题。我不认为这是预期的实现方法,但它有效。因为我知道有人会遇到同样的问题。
string path = AppDomain.CurrentDomain.BaseDirectory;
string filePath = $"{path}EmailTemplates\\ExceptionEmail.cshtml";发布于 2018-07-31 23:30:50
所以我今天在生产中确认了修复。添加这2行确实纠正了这个问题。因此,它的完整代码块是
var config = new TemplateServiceConfiguration
{
TemplateManager = new ResolvePathTemplateManager(new[] {"EmailTemplates"}),
DisableTempFileLocking = true
};
string path = AppDomain.CurrentDomain.BaseDirectory;
string filePath = $"{path}EmailTemplates\\ExceptionEmail.cshtml";
Engine.Razor = RazorEngineService.Create(config);
var emailHtmlBody = Engine.Razor.RunCompile(filePath, null, model);发布于 2020-07-03 15:59:58
有同样的问题,我的解决方案是更改项目文件ProjectName.csproj外部,并将视图作为内容添加,而不是从
<None Include="Views/Folder/Name.cshtml"/>至
<Content Include="Views\Emails\Name.cshtml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>https://stackoverflow.com/questions/51449148
复制相似问题