TemplateServiceConfiguration templateConfig = new TemplateServiceConfiguration { BaseTemplateType = typeof(HtmlTemplateBase<>) }; Razor.SetTemplateService(new TemplateService(templateConfig)); string template = "This is my sample template, Hello @Href()"; Razor.Compile(template,typeof(TestModel),"aa"); Console.WriteLine(Razor.Run("aa",new TestModel() { Name = "World"}));
public class HtmlTemplateBase<T>: TemplateBase<T> { public string Href() { var m = Model as TestModel; return m.Name.ToUpper(); } } public class TestModel { public string Name { get; set; } }
修改Razorjs,不过他们引用的RazorEngine,太老的针对的是mvc3的,很多类都不对,去codeplex看了下,现在也搬家去github了。另外Razorjs也不支持Session,到时候还要改下
主要是RazorEngine v2.1的设置模板的方式到3.0的时候变了
v2Razor.SetTemplateBase(typeof(MyCustomTemplateBase<>));v3
Razor.SetTemplateService
RazorEngine
时间: 2024-10-17 03:00:42