> ## Content Index
> Fetch the complete content index at: https://davidzych.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# View compilation in ASP.NET 5 with the RazorPreCompileModule
- URL: https://davidzych.com/view-compilation-in-aspnet-5-with-the-razorprecompilemodule/
- Published: 2015-10-14T20:58:03.000Z
- Updated: 2026-11-19T17:22:13.000Z
- Author: David Zych
- Tags: asp.net 5, roslyn, mvc 6, razor, view, #Import 2026-11-20 21:02

In MVC versions up to and include version 5, you could compile your Razor views by adding the `<MvcBuildViews>` attribute in the `csproj` file. But in ASP.NET 5 there is no more csproj file, so how we compile views for MVC 6?

## Why?

If you've never compiled views and are wondering why you would, the reason is that it gets you compile-time checking. This means if you change your view model, you'll know if you broke the bindings in your view during compilation of your project. The alternative is waiting until run-time and until you view the page which could easily be overlooked. Sooner is always better.\*

## How?

The short answer is: Create a class that inherits from `RazorPreCompileModule` and place it in the `{root}\compiler\preprocess`\*\* folder of your application.

```
namespace RazorPrecompileTest.compiler.preprocess
{
    public class RazorPreCompilation : RazorPreCompileModule
    {
    }
}

```

And that's it! Whenever you build your application your razor views will also be compiled and you'll get compile time checking on them. If you want to know how it works, read on...

## So... how does it work?

#### RazorPreCompileModule

[RazorPreCompileModule](https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNet.Mvc.Razor/Precompilation/RazorPreCompileModule.cs?ref=davidzych.com) is an abstract class that implements the [assembly neutral interface](https://davidzych.com/what-is-an-assembly-neutral-interface-and-why-do-we-need-it/) `ICompileModule`.

```
public abstract class RazorPreCompileModule : ICompileModule 

```

#### ICompileModule

```
public interface ICompileModule
{
    void BeforeCompile(BeforeCompileContext context);
    void AfterCompile(AfterCompileContext context);
}

```

[ICompileModule](https://github.com/aspnet/dnx/blob/dev/src/Microsoft.Dnx.Compilation.CSharp.Abstractions/ICompileModule.cs?ref=davidzych.com) is an interface that [*"allows plugging into the compilation pipeline"*](https://github.com/aspnet/dnx/blob/dev/src/Microsoft.Dnx.Compilation.CSharp.Abstractions/ICompileModule.cs?ref=davidzych.com#L7). During compilation of your project, dnx will use [Roslyn](https://davidzych.com/microsoft-releases-a-preview-of-the-net-compiler-platform-codenamed-roslyn/) to build up an [in memory assembly appended with !preprocess](https://github.com/aspnet/dnx/blob/dev/src/Microsoft.Dnx.Compilation.CSharp/RoslynCompiler.cs?ref=davidzych.com#L230) with the same references as your project. It will use that to [look for any and all classes that implement ICompileModule](https://github.com/aspnet/dnx/blob/dev/src/Microsoft.Dnx.Compilation.CSharp/RoslynCompiler.cs?ref=davidzych.com#L234) and will run the `BeforeCompile` and `AfterCompile` methods for each class, before and after compilation, respectively.

---

\*[Well, most of the time](http://www.urbandictionary.com/define.php?term=minute+man&ref=davidzych.com)

\*\*[It **must** be put in that directory!](https://github.com/aspnet/dnx/blob/dev/src/Microsoft.Dnx.Runtime/ProjectFilesCollection.cs?ref=davidzych.com#L16)