如何将自定义 templateBase 设置为基本模板

How to set custom templateBase as base template

我使用 TemplateBase 制作了自定义模板。如何将我的模板设置为剃刀基本模板。我设法使用旧的 api 做到了这一点,但我在缓存方面遇到了问题。在新的 api 中,缓存似乎更容易,但我找不到任何将自己的模板设置为基本模板的示例。

在您的启动程序或类似程序中,添加以下内容

var templateConfig = new TemplateServiceConfiguration
{
    BaseTemplateType = typeof(YourCustomTemplateBase<>)
};

var service = RazorEngineService.Create(templateConfig);

Engine.Razor = service;

添加您的模板库应创建为

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