FluentEmail
FluentEmail copied to clipboard
Doesn't work with models
I am using .NET Core 2 and added the following to my csproj file
<MvcRazorExcludeRefAssembliesFromPublish>false</MvcRazorExcludeRefAssembliesFromPublish>
<PreserveCompilationContext>true</PreserveCompilationContext>
I have done the following
- Created a model
- Created a razor template
- Populated the template with the model references e.g. @Model.IssueNumber
- I pass the model into template like this -
email.UsingTemplateFromFile($"{Directory.GetCurrentDirectory()}\\Features\\Shared\\EmailTemplates\\MyTemplate.cshtml", model);
When I run it I get a NullReferenceException: Object reference not set to an instance of an object.
However, if I use a basic file e.g. MyTemplate.cshtml without any references to models etc and add the content as Test ##IssueNumber## the following code works!!
email.UsingTemplateFromFile($"{Directory.GetCurrentDirectory()}\\Features\\Shared\\EmailTemplates\\MyTemplate.cshtml", new {IssueNumber = "123"});
Am I doing something wrong?
Please ensure you adding this line of code
// Using Razor templating package Email.DefaultRenderer = new RazorRenderer();
At your start up. This should work
This is related to Razorlight issues (toddams/RazorLight#147) You can use the fix in the linked issue, or wait for the next release of razorlight.
If anyone is still hitting this, I've added a temporary build of Razorlight with the linked fixes and uploaded a new FluentEmail.Razor to our pre-release myget feed (2.5.0-alpha1): https://www.myget.org/F/fluentemail/api/v3/index.json
@bjcull Any solution to this?