lukencode / FluentEmail

All in one email sender for .NET. Supports popular senders (SendGrid, MailGun, etc) and Razor templates.
https://lukelowrey.com/dotnet-email-guide-2021/
MIT License
3.04k stars 438 forks source link

Question: UsingTemplateFromEmbedded for compiled Views in ASP.NET Core 2.1 #115

Open VaclavElias opened 6 years ago

VaclavElias commented 6 years ago

Hi,

I cannot find any reference for this scenario in your Documentation.

My website name Project.Web compiles to Project.Web.dll and Project.Web.Views.dll.

I have got a standard folder structure and in Views I have got: ~/Views/Email/Alert.cshtml.

This Views folder is automatically compiled to Project.Web.Views.dll.

How can I reference it correctly through UsingTemplateFromEmbedded as the assembly actually doesn't exist at the time of development and the compiled view is not in Project.Web.dll which I could easily reference?

await email.To("my@email.com")
                .Subject("Test Email")
                .UsingTemplateFromEmbedded("Project.Web.Views.cshtml", model, assembly)
                .SendAsync();

Hope that my question makes a sense? :)

Thanks.

sguryev commented 6 years ago

I guess you should use the Default Namespace from the project properties and then add the path the to cshtml: Root.Sample.Project.Templates.MyTemplate.cshtml

sguryev commented 6 years ago

@lukencode @bjcull Guys could you please describe a best way practice for using the Embedded views? Let's say we have a library (EmailService) with all the Templates which is shared across the several ASP.NET Core 2.1 projects in the solutions. We should change the Build action of the templates to Embedded resource by adding something like

  <ItemGroup>
    <EmbeddedResource Include="Templates\**" />
  </ItemGroup>

right to the library project.

As is it will cause the runtime error like Cannot find compilation library location for package 'Microsoft.NETCore.App' after publishing. So we have to go to the published ASP.NET Core 2.1 application csproj file and add the line:

<MvcRazorExcludeRefAssembliesFromPublish>false</MvcRazorExcludeRefAssembliesFromPublish>

Which causes the publishing of the refs folder (which is pretty big) for the Razor for Embedded and not precompiled views.

Is it a best way? Maybe we shoule precompile Templates instead (since embedded resource doesn't allow to change the template like text file on the fly and requires DLL publishing for updating)?

References I have: https://stackoverflow.com/questions/51269559/razor-templates-cannot-find-compilation-library-location-for-package/51269560#51269560 https://github.com/toddams/RazorLight/issues/203 https://github.com/aspnet/Mvc/issues/6021