Catch Errors in ASP.NET MVC Views

The compiler is my friend. It helps me check for simple spelling mistakes and alerts me when a change in one part of the code base breaks a reference somewhere else.

When developing in ASP.NET Web Forms I have had a hard time with the lack of  compilation of aspx pages. As long as the code is in the code behind,  errors are catched during compilation. If the code is in the aspx file itself (e.g. data binding code) it doesn’t fail until the page is accessed. A large application with hundreds of pages requires detailed testing to make sure all pages have been accessed.

In ASP.NET MVC (which I really recommend instead of Web Forms) there is a far better way. The views can be included in the project build, catching any simple mistakes when building. Unfortunately it is not a standard setting, but it is easy to add.

Enabling MVC Views Compilation

To enable compilation of the MVC Views during project build, you have to edit the project file manually.

  • Right click the project and choose “unload”.
  • Right click the project and choose “edit <project file>”.
  • Find the line with <MvcBuildViews>false</MvcBuildViews>.
  • Change the line to <MvcBuildViews>true</MvcBuildViews>.
  • Save the project file.
  • Right click the project and choose “reload <project>”.

Now whenever the project is built, all views are compiled as well.


Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.