如何在非 MVC 项目中为 RazorTemplateEngine 创建 RazorEngineHost
How to create RazorEngineHost for RazorTemplateEngin in non MVC project
我在控制台应用程序中使用 RazorEngine
。
我想初始化 RazorEngineHost
并传入 RazorTemplateEngin
。然而,正如我在 MSDN 文档中看到的那样,它说 RazorEngineHost
"is not intended to be used directly from your code".RazorEngineHost
那么创建 RazorTemplateEngin
的最佳方法是什么?
这个问题与 How to use Razor View Engine in a console application? 完全不同>我想使用 RazorTemplateEngin 因为我有模板而不是字符串。
如果你看看RazorTemplateEngine on MSDN you can see that the documentation for that also says "not intended to be used directly from your code", that doesn't mean that you can't. If you want to use an instance of RazorTemplateEngine
you have to pass in a RazorEngineHost
instance. Have a look at this blog post for usage: Leveraging Razor Templates Outside of ASP.NET
var language = new CSharpRazorCodeLanguage();
var host = new RazorEngineHost(language) {
DefaultBaseClass = "OrderInfoTemplateBase",
DefaultClassName = "OrderInfoTemplate",
DefaultNamespace = "CompiledRazorTemplates",
};
host.NamespaceImports.Add("System");
To begin, the RazorEngineHost’s constructor accepts a RazorCodeLanguage specifying the target template’s code language. This example produces a host that can parse Razor templates written using C#. To support templates written in Visual Basic, supply a VBRazorCodeLanguage instance instead. The additional initializer properties instruct the code generator to emit code with a particular class name, deriving from a custom template base class, and residing in a particular namespace. Finally, add the System namespace to the list of imported namespaces required for the generated class to compile just as you would import a namespace in a normal, hand-written class.
我在控制台应用程序中使用 RazorEngine
。
我想初始化 RazorEngineHost
并传入 RazorTemplateEngin
。然而,正如我在 MSDN 文档中看到的那样,它说 RazorEngineHost
"is not intended to be used directly from your code".RazorEngineHost
那么创建 RazorTemplateEngin
的最佳方法是什么?
这个问题与 How to use Razor View Engine in a console application? 完全不同>我想使用 RazorTemplateEngin 因为我有模板而不是字符串。
如果你看看RazorTemplateEngine on MSDN you can see that the documentation for that also says "not intended to be used directly from your code", that doesn't mean that you can't. If you want to use an instance of RazorTemplateEngine
you have to pass in a RazorEngineHost
instance. Have a look at this blog post for usage: Leveraging Razor Templates Outside of ASP.NET
var language = new CSharpRazorCodeLanguage();
var host = new RazorEngineHost(language) {
DefaultBaseClass = "OrderInfoTemplateBase",
DefaultClassName = "OrderInfoTemplate",
DefaultNamespace = "CompiledRazorTemplates",
};
host.NamespaceImports.Add("System");
To begin, the RazorEngineHost’s constructor accepts a RazorCodeLanguage specifying the target template’s code language. This example produces a host that can parse Razor templates written using C#. To support templates written in Visual Basic, supply a VBRazorCodeLanguage instance instead. The additional initializer properties instruct the code generator to emit code with a particular class name, deriving from a custom template base class, and residing in a particular namespace. Finally, add the System namespace to the list of imported namespaces required for the generated class to compile just as you would import a namespace in a normal, hand-written class.