RazorEngine 无法解析模板

RazorEngine Could not resolve template

我有一个控制台应用程序,它使用 RazorEngine 根据应用程序的 运行 发送电子邮件。我成功地能够通过可执行文件在开发 IDE 和 运行 控制台应用程序中发送电子邮件。但是,当我 运行 来自计划任务的控制台应用程序时,我在线收到错误 "Could not resolve template",

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 中做同样的事情。我把它放在 GitHub as RazorEngine_Test

更新 4:

TemplateServiceConfiguration 似乎没有按设计工作,或者我处理的示例不正确。我用下面的两行更新了测试项目,这解决了我的问题。我不认为这是预期的实施方法,但它有效。发帖是因为我知道有人会 运行 遇到同样的问题。

string path = AppDomain.CurrentDomain.BaseDirectory;
string filePath = $"{path}EmailTemplates\ExceptionEmail.cshtml";

所以我今天能够在生产环境中确认修复。添加这两行确实解决了问题。所以完整的代码块是

 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);

有同样的问题我的解决方案是更改项目文件 ProjectName.csproj 外部并将视图添加为内容而不是 None 来自

    <None Include="Views/Folder/Name.cshtml"/>

    <Content Include="Views\Emails\Name.cshtml">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>