MSBuildRazorCompiler 0.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package MSBuildRazorCompiler --version 0.1.0                
NuGet\Install-Package MSBuildRazorCompiler -Version 0.1.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MSBuildRazorCompiler" Version="0.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MSBuildRazorCompiler --version 0.1.0                
#r "nuget: MSBuildRazorCompiler, 0.1.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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:

  1. Compilation of .cshtml files to .cs files - this happens just before the CoreCompile task of the project you install this library into via the included MSBuild task.
  2. 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

  1. Include SomeFile.cshtml files in your (.netcore3.1) project
  2. Install-Package MSBuildRazorCompiler` (work-in-progress, not in nuget.org yet)
  3. Compile - you should now see some-file.cshtml.generated.cs next to it with a class SomeFile in the namespace of your project
  4. Execute the following code to get a rendered result: new SomeFile().Render(model, viewBag or null) - there is also an async variant
Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
Version Downloads Last updated
0.3.1 1,592 4/19/2020
0.3.0 518 4/19/2020
0.2.1 535 4/18/2020
0.2.0 504 4/14/2020
0.1.2 505 4/13/2020
0.1.1 517 4/13/2020
0.1.0 535 4/13/2020