![]() |
VOOZH | about |
Use RazorBlade for build-time compilation and RazorLight for run-time compilation
dotnet add package MiniRazor.Runtime --version 2.2.0
NuGet\Install-Package MiniRazor.Runtime -Version 2.2.0
<PackageReference Include="MiniRazor.Runtime" Version="2.2.0" />
<PackageVersion Include="MiniRazor.Runtime" Version="2.2.0" />Directory.Packages.props
<PackageReference Include="MiniRazor.Runtime" />Project file
paket add MiniRazor.Runtime --version 2.2.0
#r "nuget: MiniRazor.Runtime, 2.2.0"
#:package MiniRazor.Runtime@2.2.0
#addin nuget:?package=MiniRazor.Runtime&version=2.2.0Install as a Cake Addin
#tool nuget:?package=MiniRazor.Runtime&version=2.2.0Install as a Cake Tool
👁 Build
👁 Coverage
👁 Version
👁 Downloads
👁 Discord
👁 Donate
⚠️ Project status: maintenance mode (bug fixes only).
MiniRazor is a tiny abstraction over the Razor engine, designed to provide a simple interface to compile and render templates, both at build time and at runtime.
💬 If you want to chat, join my Discord server.
All-in-one meta package:
dotnet add package MiniRazorSpecialized packages:
dotnet add package MiniRazor.Compiler (runtime compilation only)dotnet add package MiniRazor.CodeGen (build time compilation only)⚠ If you're referencing MiniRazor.CodeGen, ensure that it's NOT marked with
PrivateAssets="all"! Although the source generator assembly itself is only used during build, this package also contains binaries which are required by the generated code at runtime.
⚠️ Compiling at build time requires MiniRazor.CodeGen.
MiniRazor comes with a source generator that can parse Razor templates and transpile them into C# classes directly at build time. This workflow is suitable and highly recommended for scenarios where your templates are not expected to change.
To do that, first create a Razor template as shown here:
@inherits MiniRazor.TemplateBase<string>
@namespace MyNamespace.Templates
<html>
<head>
<title>Hello @Model</title>
</head>
<body>
<p>Hello @Model</p>
</body>
</html>
Note the usage of two important directives at the top of the file:
@inherits directive indicates that the base type of this template is MiniRazor.TemplateBase<TModel>, with the model of type string.
If this directive is not included, the template will instead inherit from MiniRazor.TemplateBase<dynamic> by default, which doesn't offer the same level of type-safety when working with the model.
@namespace directive instructs the compiler to put the generated class into the MyNamespace.Templates namespace.
If this directive is omitted, the default namespace of MiniRazor.GeneratedTemplates will be used instead.
In order to make the template accessible by MiniRazor's source generator, you need to add it to the project as AdditionalFiles and mark it with the IsRazorTemplate="true" attribute:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<AdditionalFiles Include="Templates/TemplateFoo.cshtml" IsRazorTemplate="true" />
<AdditionalFiles Include="Templates/*.cshtml" IsRazorTemplate="true" />
</ItemGroup>
</Project>
Once that's done, you should be able to run dotnet build to build the project and trigger the source generator.
Given that the template's file name is TemplateFoo.cshtml, the generated class should be accessible as MyNamespace.Templates.TemplateFoo.
To render it, you can call the RenderAsync(...) static method:
// Reference the namespace where the template is located
using MyNamespace.Templates;
// Render the template to string, with @Model set to "world"
var output = await TemplateFoo.RenderAsync("world");
// Or, alternatively, render it to the specified TextWriter
await TemplateFoo.RenderAsync(Console.Out, "world");
Note that the type of the model parameter in RenderAsync(...) is automatically inferred from the @inherits directive specified in the template.
Here, since the template is derived from MiniRazor.TemplateBase<string>, the method expects a parameter of type string.
⚠️ Compiling at runtime requires MiniRazor.Compiler.
If the previous approach doesn't fit your usage scenario, you can also compile templates at runtime.
To do that, call Razor.Compile(...) with the template's source code:
using MiniRazor;
// Compile the template into an in-memory assembly
var template = Razor.Compile("<p>Hello, @Model.Subject!</p>");
// Render the template to string
var output = await template.RenderAsync(new MyModel { Subject = "World" });
// <p>Hello, World!</p>
Calling Razor.Compile(...) transforms the provided Razor template directly into IL code and hosts it in a generated in-memory assembly.
This returns an instance of TemplateDescriptor, which can then be used to render output against a model.
By default, MiniRazor uses the default assembly load context, which means that the compiled IL code will stay in memory forever.
To avoid that, you can pass a custom instance of AssemblyLoadContext that lets you control the lifetime of generated assemblies:
// Create an isolated assembly load context
var alc = new AssemblyLoadContext("MyALC", true);
// Compile the template
var template = Razor.Compile("<p>Hello, @Model.Subject!</p>", alc);
// Unload the ALC once it's no longer needed
alc.Unload();
Output rendered with Razor templates is HTML-encoded by default.
If you want to print raw HTML content, for example if it's sourced from somewhere else, you can use the Raw(...) method:
@{
string GetHtml() => "<p>Hello world!</p>";
}
@GetHtml() // <p>Hello world!</p>
@Raw(GetHtml()) // <p>Hello world!</p>
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 2 NuGet packages that depend on MiniRazor.Runtime:
| Package | Downloads |
|---|---|
|
MiniRazor.CodeGen
Contains Roslyn source generator that transpiles Razor templates into C# classes |
|
|
MiniRazor.Compiler
Contains methods to transpile and compile Razor templates |
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated | |
|---|---|---|---|
| 2.2.0 | 9,844 | 10/7/2021 | 2.2.0 is deprecated because it is no longer maintained. |
| 2.1.4 | 5,743 | 7/19/2021 | 2.1.4 is deprecated because it is no longer maintained. |
| 2.1.3 | 3,040 | 7/19/2021 | 2.1.3 is deprecated because it is no longer maintained. |
| 2.1.2 | 21,498 | 3/22/2021 | 2.1.2 is deprecated because it is no longer maintained. |
| 2.1.1 | 2,930 | 3/22/2021 | 2.1.1 is deprecated because it is no longer maintained. |
| 2.1.0 | 3,120 | 3/22/2021 | 2.1.0 is deprecated because it is no longer maintained. |