Compiled Razor Views

The primary benefits of compiled views is improved performance by eliminating compile times of Razor views. They can also provide static compilation benefits by highlighting compile errors during development and can simplify deployment by avoiding the need to deploy any *.cshtml files as they end up pre-compiled in the containing Assembly.

Install ServiceStack.Razor.BuildTask

To enable compiled razor views you need to add the ServiceStack.Razor.BuildTask NuGet Package to the project containing your Razor *.cshtml pages, i.e:

<PackageReference Include="ServiceStack.Razor.BuildTask" Version="5.*" />

This doesn't add any additional dlls to your project, instead it just sets the BuildAction to all *.cshtml pages to Content and registers an MSBuild task to your .csproj project file set to pre-compile razor views on every build.

Register Compiled Assembly to RazorFormat Plugin

To register assemblies containing compiled razor views with Razor Format you just need to add it to RazorFormat.LoadFromAssemblies, e.g:

Plugins.Add(new RazorFormat {
    LoadFromAssemblies = { typeof(RockstarsService).Assembly }

Retains optimal development workflow

The Compiled Views support continues to retain a great development experience in DebugMode as all Razor Views are initially loaded from the Assembly but also continues to monitor the file system for modified views, automatically compiling and loading them on the fly so AppDomain reloads aren't required to see changes.

Example Projects

Razor Rockstars

The RazorRockstars.CompiledViews VS.NET project shows an example of Razor Rockstars which uses shared compiled Razor Views in a .dll in a number of different projects:


The ServiceStack.Gap project shows how to extend Compiled Razor Views and use them to create embedded ServiceStack solutions that can be ILMerged down to a single .exe.