Can Razor views be compiled?

Edit:

Here is a blog post on this topic as well:

How to Detect Errors of Our ASP.NET MVC Views on Compile Time

To make your views to be compiled, do the following;

  1. Unload your project by right
    clicking the project on the solution
    explorer in VS and clicking unload
    project
  2. right click the project which has
    been converted to unavailable
    project and click “Edit
    your_project_name.csproj” (that
    would be .vbproj if your
    project is VB project)
  3. see the following code;

    <!--There some lines of code here and I deleted them to get to the point quickly-->
    
    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
    <MvcBuildViews>false</MvcBuildViews>
    

  4. change the MvcBuildViews tag value from false to true

  5. after that save it and reload your
    project.

after you build your solution to compile it, you will see that your view will be compiled too.

NOTE: to test it, break some code in one of your view on purpose and try to build. you will see that you’ll get an error message.

Leave a Comment