Skip to content

Using RazorGenerator.MsBuild

Ken Dale edited this page Mar 13, 2018 · 4 revisions

Unlike the Vsix which depends on Visual Studio's single file generator tooling, RazorGenerator.MsBuild allows precompiling views independent of Visual Studio. It works by dynamically generating C# files for Views during build which are then compiled like ordinary files in the project.

Installing the package

The package depends on NuGet's target import feature that was introduced in NuGet 2.5. Additionally, having auto package restore greatly simplifies dealing with not-checking in packages. It is highly recommended upgrading to NuGet v.2.7 or higher when using this package.

To use the package, install the package using NuGet.

Install-Package RazorGenerator.MsBuild

Verifying if it works

To verify that the Msbuild target has been set up correctly, check if files are being generated under the obj\CodeGen directory of your application.

Customizing the target

The target file assumes some default values for properties and itemgroups. These values can be overridden from your csproj

  • RazorSrcFiles ItemGroup - the files that the generator precompiles. Defaults to any cshtml file under the project's directory. For instance, to customize it to only include files under the Views directory, add this ItemGroup to your csproj.
    <RazorSrcFiles Include="Views\**\*.cshtml" />

If you have already included the files in your project, prevent including them twice by setting the InProject element:

    <RazorSrcFiles Include="Views\**\*.cshtml">
  • RazorViewsCodeGenDirectory - the directory at which files are precompiled to. Defaults to obj/CodeGen directory. You can change this by specifying a value for this property from your csproj

Note: Property and Item group declarations must occur before the msbuild target Import

You can’t perform that action at this time.