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 Reply

    Your name as it will be displayed on the posted comment.
    Your e-mail address will not be published. It is only used if I want to get in touch during comment moderation.
    Your name will be a link to this address.
Software Development is a Job – Coding is a Passion

I'm Anders Abel, an independent systems architect and developer in Stockholm, Sweden.

profile for Anders Abel at Stack Overflow, Q&A for professional and enthusiast programmers

Code for most posts is available on my GitHub account.

Popular Posts

Archives

Series

Powered by WordPress with the Passion for Coding theme.