FluentEmail: Layouts with RazorLight cannot be resolved
Hi, I can’t use layouts in my project. Whatever I do it always ends up with
Can not resolve a content for the template "{0}" as there is no project set.You can only render a template by passing it's content directly via string using coresponding function overload
I keep all my emails in the same project inside of directory Emails (like Emails/AccountCreated.cshtml). My method looks like that:
private async Task SendEmail(EmailMessage message, CancellationToken cancellationToken)
{
var templatePath = $"{Directory.GetCurrentDirectory()}/Emails/{message.TemplateName}.cshtml";
await _email
.To(message.To)
.Subject(message.Subject)
.UsingTemplateFromFile(templatePath, message.Model)
.SendAsync(cancellationToken);
}
Example page:
@{
Layout = "Layout.cshtml";
}
<h1>Hello World</h1>
Layout.cshtml:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
<link href="/css/site.css" rel="stylesheet" type="text/css" />
</head>
<body>
@RenderBody()
</body>
</html>
Help 🤷♂️
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 2
- Comments: 17 (2 by maintainers)
Some hints if you’re gonna encourage this problem:
Thanks guys again. Have a good day!
We have the same issue. Looks like pull request #145 adds support for layouts but it’s not in a release or pre release yet. Would it be possible to push a new release?
I’m happy to help with deployment via azure devops if thats the plan.
It’s arguably abstraction overkill that FluentEmail provides much opinion at all about RazorLight API calling conventions. I’m not sure there is a ton of benefit to hiding RazorLight as a transitive dependency in FluentEmail vs. letting downstream consumers plug in RazorLight.
@rafalschmidt97 , I’m using EmbeddedResources as my email templates and I was adding razor render using DI like this:
After checking the unit tests of FluentEMail Razor and RazorLight doc I figured out that I should pass
typeof(rootType)
toin order that my code works properly, without doing this RazorLight can’t find the TemplateKey.
In your case I think you should pass a RootPath to you RazorRenderer, bellow the code taken from FluentEmail Razor Unit Tests:
@rafalschmidt97 Thank you so much for your reply above. This worked for me. I appreciate the time you spent doing this for us.
I went with an approach that just parses a Header partial and prepends it and Footer partial that is appended to the template string. Works for with the current released version.