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(http://msdn.microsoft.com/en-us/library/646dk05y%28v=VS.80%29.aspx) to your csproj.
<ItemGroup>
    <RazorSrcFiles Include="Views\**\*.cshtml" />
</ItemGroup>
  • RazorViewsCodeGenDirectory- the directory at which files are precompiled to. Defalts to obj/CodeGen directory. You can change this by specifying a value for this property from your csproj
<RazorViewsCodeGenDirectory>$(Temp)\Myproject\RazorTempFiles\/<RazorViewsCodeGenDirectory>

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

Last edited Aug 26, 2013 at 2:34 PM by pranavkm, version 6

Comments

jeffmoser Jun 18, 2013 at 4:25 PM 
To get this to work, be sure to change your import to the latest version, as in:

<Import Project="$(SolutionDir)\packages\RazorGenerator.MsBuild.2.0.1\tools\RazorGenerator.targets"/>

However, be sure to put this import towards the *end* of your .csproj or else it the "CompileDependsOn" that it adds will get overridden. Also, make sure you set the "<PrecompileRazorFiles>true</PrecompileRazorFiles>" before you do the import.

Once you do this, you can use the RazorGenerator.Mvc nuget package just fine.

amhed Jun 9, 2013 at 5:25 AM 
I just tried this. I changed the reference for the latest nuget package to this:
<Import Project="$(SolutionDir)\packages\RazorGenerator.MsBuild.2.0.1\tools\RazorGenerator.targets"/>

But it doesn't work at all....

thecubical Feb 12, 2013 at 12:05 AM 
The import is slightly incorrect it should be
<Import Project="$(SolutionDir)\packages\RazorGenerator.MsBuild.1.5.0.0\tools\RazorGenerator.targets"/>

The version is missing the 0.0