如何解决项目找不到带键 _Layout.cshtml 的模板
在处理 _Layout.cshtml
中 FluentEmail
的嵌入式资源时,我正在努力使电子邮件正常工作。
这是我得到的异常错误:
Project can not find template with key _Layout.cshtml
这是我目前的设置:
在 Program.cs
的 ConfigureServices 中,我将 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() { });
在我的 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.");
}
}
SomeReport.cshtml
位于 Views\Emails\SomeReport.cshtml
内,如下所示:
@using System;
@using RazorLight;
@using System.Collections.Generic;
@using AppName.Models;
@inherits TemplatePage<IEnumerable<SomeReport>>
@{
@* For working with Embedded Resource Views *@
Layout = "_Layout.cshtml";
}
@* Work with the Model here... *@
_Layout.cshtml
位于 Views\Shared\_Layout.cshtml
内,如下所示:
@using RazorLight;
@* Some common layout styles here *@
@RenderBody()
SomeReport.cshtml
和 _Layout.cshtml
都是嵌入式资源:
我的参考文献有 RazorLight
到 FluentEmail.Razor
包。
如果有帮助,这是一个 .NET 5 Worker Service
项目,我还在 PreserveCompilationContext
文件中添加了 PreserveCompilationReferences
和 .csproj
:
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<PreserveCompilationContext>true</PreserveCompilationContext>
<PreserveCompilationReferences>true</PreserveCompilationReferences>
</PropertyGroup>
我已经到处寻找,但仍然没有找到解决方案。这么简单的事情是很难完成的。请帮忙。
谢谢!
解决方法
在您的模板中而不是:
Layout = "_Layout.cshtml"
试试:
Layout = ""./Shared/_Layout.cshtml""
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。