toddams / RazorLight

Template engine based on Microsoft's Razor parsing engine for .NET Core
Apache License 2.0
1.52k stars 259 forks source link

RazorLight: Project cannot find template with key ForgotPassword.cshtml. #507

Closed liamkavfc closed 1 year ago

liamkavfc commented 1 year ago

Describe the bug I am running into the error that is mentioned in the title.

When I list my embedded files, the file in question is the only one in the list, but the error still remains.

      `string[] files = typeof(QueueTrigger).Assembly.GetManifestResourceNames();

        foreach (string file in files)
        {
            Console.WriteLine(file);
        }`

        `var engine = new RazorLightEngineBuilder()
                 .UseEmbeddedResourcesProject(typeof(QueueTrigger).Assembly, "EmailFunction.EmailTemplates")
                 .UseMemoryCachingProvider()
                 .Build();

         var rendered = await engine.CompileRenderAsync("ForgotPassword", messageDto);`

System.Private.CoreLib: Exception while executing function: QueueTrigger. RazorLight: Project can not find template with key ForgotPassword.cshtml.

To Reproduce Steps to reproduce the behavior: Follow README.md steps 'Embedded Resource'

Expected behavior I would expect the error to not be there after following the README

Information (please complete the following information):

Additional context I am implementing this in an Azure function. I have read the README.md section on this and have jumped this hurdle, I feel like the key not found is the last step.

liamkavfc commented 1 year ago

Fixed this issue!

var engine = new RazorLightEngineBuilder()
              .UseEmbeddedResourcesProject(typeof(QueueTrigger).Assembly)
              .SetOperatingAssembly(typeof(QueueTrigger).Assembly)
              .UseMemoryCachingProvider()
              .Build();
var rendered = await engine.CompileRenderAsync("EmailTemplates.ForgotPassword", messageDto);

Adding SetOperatingAssembly() fixed it! 👍