Search code examples
c#.netrazorengine

How to set custom templateBase as base template


I made custom template using TemplateBase. How to set my template as razor base template. I managed to do this using old api but than I had problem with caching. In new api caching seems much easier but I can't find any example of setting own template as base template.


Solution

  • In your start up routine or similar, add the following

    var templateConfig = new TemplateServiceConfiguration
    {
        BaseTemplateType = typeof(YourCustomTemplateBase<>)
    };
    
    var service = RazorEngineService.Create(templateConfig);
    
    Engine.Razor = service;
    

    Add your template base should be created as

    public abstract class YourCustomTemplateBase<T> : TemplateBase<T>
    {
        public string CustomString { get; set; }
    }