Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AddDynamicTemplates for embedded resources #534

Open
sybaris opened this issue Nov 10, 2023 · 1 comment
Open

AddDynamicTemplates for embedded resources #534

sybaris opened this issue Nov 10, 2023 · 1 comment

Comments

@sybaris
Copy link

sybaris commented Nov 10, 2023

Is your feature request related to a problem? Please describe.
I want to use Layout but with embedded resources.
Currenlty, I use RazorLight without layouts. It works.

My current code is

        internal static string GenerateHtml<T>(string resourceName, T model)
        {
            var engine = new RazorLightEngineBuilder()
                .UseEmbeddedResourcesProject(typeof(RootTypeForEmbeddedResources).Assembly, typeof(RootTypeForEmbeddedResources).Namespace)
                //.AddDynamicTemplates(new Dictionary<string,string>()
                //{ {"_Layout.cshtml","<body>Test\r\n    @RenderBody()\r\n</body>" }}
                //)
                .SetOperatingAssembly(operatingAssembly)
                .UseMemoryCachingProvider()
                .EnableDebugMode()
                .Build();

            string html = engine.CompileRenderAsync<T>(resourceName, model).Result;
            return html;
        }

I test, if I use AddDynamicTemplates (commented code), it works too.
No I want to have a file "_Layout.cshtml" which is an embedded ressource.
Currently if I do nothing, I have the following Exception : "One or more errors occurred. (RazorLightProjectItem of type RazorLight.Razor.EmbeddedRazorProjectItem with key _Layout.cshtml could not be found by the RazorLightProject of type RazorLight.Razor.EmbeddedRazorProject and does not exist in dynamic templates. See the "KnownDynamicTemplateKeys" and "KnownProjectTemplateKeys" properties for known template keys.) ---> RazorLight.TemplateNotFoundException: RazorLightProjectItem of type RazorLight.Razor.EmbeddedRazorProjectItem with key _Layout.cshtml could not be found by the RazorLightProject of type RazorLight.Razor.EmbeddedRazorProject and does not exist in dynamic templates. See the "KnownDynamicTemplateKeys" and "KnownProjectTemplateKeys" properties for known template keys."

And KnownDynamicTemplateKeys is empty, and KnownProjectTemplateKeys does not contains my "_Layout.cshtml"

Describe the solution you'd like
Several solutions :
1°/ Create a "UseEmbeddedResourcesTemplate" like "UseEmbeddedResourcesProject"
2°/ Create a AddDynamicTemplatesFromResources
...

Additional context

@sybaris
Copy link
Author

sybaris commented Nov 13, 2023

Hi,

So currently, I do this :


        public static string ReadEmbeddedResource(Assembly assembly, string resourceName)
        {
            using (Stream resourceStream = assembly.GetManifestResourceStream(resourceName))
            using (StreamReader reader = new StreamReader(resourceStream))
            {
                return reader.ReadToEnd();
            }
        }

        internal static string GenerateHtml<T>(string resourceName, T model)
        {
            var rootType = typeof(RootTypeForEmbeddedResources);
            var rootTypeAssembly = rootType.Assembly;
            var rootTypeNamespace = rootType.Namespace;

            var layoutResource = ReadEmbeddedResource(rootTypeAssembly, rootTypeNamespace + "._Layout.cshtml");
            var engine = new RazorLightEngineBuilder()
                .UseEmbeddedResourcesProject(typeof(RootTypeForEmbeddedResources).Assembly, typeof(RootTypeForEmbeddedResources).Namespace)
                .AddDynamicTemplates(new Dictionary<string,string>()
                    {
                        {"_Layout.cshtml",layoutResource },
                    })
                .SetOperatingAssembly(operatingAssembly)
                .UseMemoryCachingProvider()
                .EnableDebugMode()
                .Build();

            string html = engine.CompileRenderAsync<T>(resourceName, model).Result;
            return html;
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant