MSBuildRazorCompiler 0.1.0
See the version list below for details.
dotnet add package MSBuildRazorCompiler --version 0.1.0
NuGet\Install-Package MSBuildRazorCompiler -Version 0.1.0
<PackageReference Include="MSBuildRazorCompiler" Version="0.1.0" />
paket add MSBuildRazorCompiler --version 0.1.0
#r "nuget: MSBuildRazorCompiler, 0.1.0"
// Install MSBuildRazorCompiler as a Cake Addin #addin nuget:?package=MSBuildRazorCompiler&version=0.1.0 // Install MSBuildRazorCompiler as a Cake Tool #tool nuget:?package=MSBuildRazorCompiler&version=0.1.0
MSBuild Razor Compiler
This library provides a .NET Core 3.1 compatible MSBuild task that will automatically compile all .cshtml
files into .cs
files in-place and also provides you code you can call to then invoke those classes with a model and get the resulting output.
This library is inspired by RazorGenerator, which was a custom tool for Visual Studio that created a compiled .cs
file when saving a .cshtml
file in your IDE. This was handy for situations where you want to have static template files (e.g. email templates, library templates, etc.). RazorGenerator doesn't support .NET Core, hence creating this.
This library is implemented by providing a thin wrapper over the RazorLight library and breaking it up into two stages:
- Compilation of
.cshtml
files to.cs
files - this happens just before theCoreCompile
task of the project you install this library into via the included MSBuild task. - Rendering / invocation of the resultant
.cs
code
Because this library generates .cs
files you don't need to worry about compilation performance, runtime compilation errors or caching or anything like that. It also allows you to have predictability of having real classes you can reference from your code rahter rather than relying on magic strings to find your .cshtml
files and needing to either ship your .cshtml
files with your code or embed them into your dll. Using this solution those files are only used at compile time and can then be discarded.
Getting started
- Include
SomeFile.cshtml
files in your (.netcore3.1) project Install-Package
MSBuildRazorCompiler` (work-in-progress, not in nuget.org yet)- Compile - you should now see
some-file.cshtml.generated.cs
next to it with a classSomeFile
in the namespace of your project - Execute the following code to get a rendered result:
new SomeFile().Render(model, viewBag or null)
- there is also an async variant
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.1 is compatible. |
-
.NETCoreApp 3.1
- RazorLight (>= 2.0.0-beta7)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on MSBuildRazorCompiler:
Repository | Stars |
---|---|
MRCollective/ChameleonForms
Shape-shifting your forms experience in ASP.NET Core MVC
|