Project can not find template with key _Layout.cshtml for embedded layout
I'm struggling to make email work while working with embedded resource for _Layout.cshtml in FluentEmail.
This is the exception error I'm getting:
Project can not find template with key _Layout.cshtml.
This is my setup so far:
Inside the ConfigureServices in Program.cs, I've added the RazorRenderer as:
//Set email service using FluentEmail
services.AddFluentEmail("[email protected]")
.AddRazorRenderer(typeof(Program))
.AddSmtpSender("smtp.somesmtp.com", 25)
.AddSmtpSender(new System.Net.Mail.SmtpClient() { });
Inside my NotificationService.cs, I always fall into that exception block:
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 is inside Views\Emails\SomeReport.cshtml which looks like this:
@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 is inside Views\Shared\_Layout.cshtml which looks like this:
@using RazorLight;
@* Some common layout styles here *@
@RenderBody()
Both the SomeReport.cshtml and _Layout.cshtml are Embedded resources:

My references has RazorLight through FluentEmail.Razor package.

If it helps, this is a .NET Core 5 Worker Service project and I've also added PreserveCompilationContext and PreserveCompilationReferences in the .csproj file:
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<PreserveCompilationContext>true</PreserveCompilationContext>
<PreserveCompilationReferences>true</PreserveCompilationReferences>
</PropertyGroup>
I've looked at everywhere and I'm about to give up on this. Something this simple has been such a struggle to make work. Please help.
I've also posted this on StackOverflow; please consider adding FluentEmail as available tags. https://stackoverflow.com/q/68579921/8644294
Thanks!
Any update on this?
I am experiencing the exact same issue
I am having the same problem
Same problem here
I'm having the same problem. Did anyone find a solution?
nope :/
same issue :(
@FrenchMasta @mkalinski93 @Svudec @ian-dowhile @jawand
Hey Guys, great news! I fixed this yesterday. Checkout my answer on Stack overflow: https://stackoverflow.com/a/73450930/8644294