Skip to content

net5.0 sample with CustomRazorLightProject

Stefan Varga edited this page Mar 28, 2022 · 4 revisions

I would like to share my net5.0 sample with others :-)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RazorLight;
using RazorLight.Razor;

namespace razorengine_netcore
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var project = new CustomRazorLightProject();

            var excludeAssembliesNames = new string[]
            {
                typeof(System.IO.File).Assembly.FullName
            };

            var engine = new RazorLightEngineBuilder()
                .UseProject(project)
                .SetOperatingAssembly(typeof(ViewModel).Assembly)
                .ExcludeAssemblies(excludeAssembliesNames) // To prevent @using System.IO @{{File.ReadAllText(@\"/etc/passwd\");}} in template
                .UseMemoryCachingProvider()
                .Build();

            var layout = @"
                <h1>@RenderBody()</h1>
                @RenderSection(""SectionX"", false)
            ";

            var template = @"
                @{ Layout = ""layout""; }
                @section SectionX { Welcome! }
                My president @Model.Name
            ";

            var model = new ViewModel { Name = "Zuzana Caputova" };

            project.Items["layout"] = layout;
            await engine.CompileTemplateAsync("layout");

            project.Items["template"] = template;
            await engine.CompileTemplateAsync("template");

            var html = await engine.CompileRenderAsync("template", model);

            Console.WriteLine(html);
        }
    }

    public class ViewModel
    {
        public string Name { get; set; }
        public string Html { get; set; }
    }

    public class CustomRazorLightProject : RazorLightProject
    {
        public Dictionary<string, string> Items { get; } = new Dictionary<string, string>();

        public override Task<RazorLightProjectItem> GetItemAsync(string templateKey)
        {
            var template = Items[templateKey];
            return Task.FromResult<RazorLightProjectItem>(new CustomRazorLightProjectItem(templateKey, template));
        }

        public override Task<IEnumerable<RazorLightProjectItem>> GetImportsAsync(string templateKey)
        {
            return Task.FromResult(Enumerable.Empty<RazorLightProjectItem>());
        }
    }

    public class CustomRazorLightProjectItem : RazorLightProjectItem
    {
        private readonly string _content;

        public CustomRazorLightProjectItem(string key, string content)
        {
            Key = key;
            _content = content;
        }

        public override string Key { get; }

        public override bool Exists => _content != null;

        public override Stream Read()
        {
            return new MemoryStream(Encoding.UTF8.GetBytes(_content));
        }
    }

}
Clone this wiki locally