RenderRazor
A small .NET Standard library that renders Razor templates to strings.
How to installl
dotnet add package RenderRazoror
Install-Package RenderRazorHow to use
- Razor template as a string:
The first line should aways be: @inherits TemplateBase<MyModel> where MyModel is the .NET class that you want to use as the @Model. You can also use the Fully Qualified Type Name there if that helps you. :)
const string TemplateString = "@inherits TemplateBase<MyModel>\nHello @Model.Name, welcome to Razor World!";- The .NET class that you want to pass as the @Model:
var model = new MyModel
{
Name = "Cats"
};- Create a renderer:
var render = new RazorRenderer<MyModel>(TemplateString);- Call the compile method:
This is very slow. It calls both the Razor and the C# compillers. This simple example executes 100 times for ~300ms on my Intel I7-6700K Workstation. When you create a renderer, be sure to reuse it.
render.Compile();- Call the render:
This is very fast. This simple example executes 1 000 000 times for ~300ms on my Intel I7-6700K Workstation.
string result = await render.Render(model);
Assert.Equal("Hello Cats, welcome to Razor World!", result);