我在使用_Layout.cshtmlin的嵌入式资源时正在努力使电子邮件正常工作FluentEmail。
这是我收到的异常错误:
Project can not find template with key _Layout.cshtml
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止的设置:
在ConfigureServices中Program.cs,我添加了RazorRenderer以下内容:
//Set email service using FluentEmail
services.AddFluentEmail("appname@domain.com")
.AddRazorRenderer(typeof(Program))
.AddSmtpSender("smtp.somesmtp.com", 25)
.AddSmtpSender(new System.Net.Mail.SmtpClient() { });
Run Code Online (Sandbox Code Playgroud)
在我的内部NotificationService.cs,我总是陷入异常块:
private async Task SendEmailAsync<TModel>(string subject, TModel model)
{
try
{
using (var scope = _serviceProvider.CreateScope())
{
var email = await scope.ServiceProvider.GetRequiredService<IFluentEmail>()
.To(string.Join(";", _emailRecipients))
.Subject(subject)
.UsingTemplateFromEmbedded("AppName.Views.Emails.SomeReport.cshtml", model, GetType().Assembly)
.SendAsync();
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to send email. Check exception for more information."); …Run Code Online (Sandbox Code Playgroud) 我正在尝试基于嵌入的 .cshtml 文件生成 HTML:
<EmbeddedResource Include="Templates\ReportTemplate.cshtml" />
Run Code Online (Sandbox Code Playgroud)
然而,从 RazorLight 我总是得到以下异常:
RazorLight.TemplateNotFoundException: Project can not find template with key My.Namespace.Application.Templates.ReportTemplate.cshtml
at RazorLight.Compilation.RazorTemplateCompiler.CreateRuntimeCompilationWorkItem(String templateKey)
at RazorLight.Compilation.RazorTemplateCompiler.OnCacheMissAsync(String templateKey)
at RazorLight.Compilation.RazorTemplateCompiler.CompileAsync(String templateKey)
at RazorLight.EngineHandler.CompileTemplateAsync(String key)
at RazorLight.EngineHandler.CompileRenderAsync[T](String key, T model, ExpandoObject viewBag)
Run Code Online (Sandbox Code Playgroud)
但是当我检查嵌入资源时,我可以清楚地看到嵌入资源:
string[] files = Assembly.GetExecutingAssembly().GetManifestResourceNames();
Run Code Online (Sandbox Code Playgroud)
files包含:My.Namespace.Application.Templates.ReportTemplate.cshtml
这是我生成 PDF 的代码:
private async Task<string> RenderRazorTemplate(string key, Type viewModelType, object viewModel)
{
string[] files = Assembly.GetExecutingAssembly().GetManifestResourceNames();
var engine = new RazorLightEngineBuilder()
.UseEmbeddedResourcesProject(Assembly
.GetExecutingAssembly()) // Use the Executing Assembly as project that embeds the …Run Code Online (Sandbox Code Playgroud)