FluentEmail icon indicating copy to clipboard operation
FluentEmail copied to clipboard

Project can not find template with key _Layout.cshtml for embedded layout

Open akhanalcs opened this issue 4 years ago • 3 comments

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!

akhanalcs avatar Jul 29 '21 16:07 akhanalcs

Any update on this?

I am experiencing the exact same issue

FrenchMasta avatar Jan 24 '22 14:01 FrenchMasta

I am having the same problem

mkalinski93 avatar Mar 03 '22 08:03 mkalinski93

Same problem here

Svudec avatar Apr 04 '22 15:04 Svudec

I'm having the same problem. Did anyone find a solution?

ian-dowhile avatar Aug 17 '22 09:08 ian-dowhile

nope :/

mkalinski93 avatar Aug 17 '22 13:08 mkalinski93

same issue :(

jawand avatar Aug 22 '22 04:08 jawand

@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

akhanalcs avatar Aug 23 '22 14:08 akhanalcs