If you have built an ASP.NET web forms site of any substance, you have undoubtedly been bitten when code in the web form becomes outdated due to code change. It’s easy to see when the page is open in Visual Studio, as the code has red squiggles and the error shows up in the Error List tool window. As soon as the page is closed however, that error disappears. Even running a build will be successful. For some of my sites, which have over 150 pages, this can be very annoying to say the least.
ASP.NET MVC has a neat hidden feature to solve this problem. The MVC team was most likely were stung by the same issue more than once. This feature is static page checking during compilation. Unfortunately, instead of being an option in the Properties window for the project (future release idea?), it is in the *.*proj xml file itself. To enable this feature:
- Right-click on the Project node in the Solution Explorer tool window
- Choose “Unload Project” - Note: If your project is under version control, you will be prompted to do a check-in. Simply click “Continue” to ignore the check-in request.
- Right-click the Project node again
- Choose “Edit *.*proj", and the project file will now open in Visual Studio with color coding and IntelliSense
- In the first PropertyGroup node, there is a new node named MvcBuildViews
- By default this is set to false. Simply change the value to true
- Right-click the Project node and choose “Reload Project”
- If prompted that the file is already open, click yes to close the xml view.
- Build your project
You will notice that builds take longer than they did prior to this change. This is because under the covers, MSBuild is now making an additional call to aspnet_compiler in the AfterBuild MSBuild target:
C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_compiler.exe -v temp -p "[physical path]"
To avoid this delay in day-to-day development, you may choose to copy the MvcBuildViews node into the build-specific PropertyGroup nodes, and set it to false for Debug and true for Release.
Update: @jglozano points out that this only works with the default WebForms View Engine.
Hope this helps