ASP.NET MVC Compiled Views – Use Them
Posted by: Malcolm Sheridan
in Category ASP.NET MVC
Abstract: The following article demonstrates how to use the compiled views feature in ASP.NET MVC.
Compiled views in ASP.NET MVC aren’t widely known but they’re a feature you should use whenever you build a non Debug version of your website. They check for compile time errors when you compile your website. They’re really useful for non debug versions, so you’d only want to enable this feature when you’re compiling in Release mode, or another configuration similar to that. This feature will only check your views for invalid syntax. It won’t check for invalid HTML.
Open studio 2010 and create a new ASP.NET MVC 3 Web Application project. Currently there’s no easy way to enable compiled views through Visual Studio without opening the project file. Hopefully this can be fixed in a future version. To add this feature, right click the project and choose Unload Project.
This will leave you with an empty project. Next right click the project file and choose Edit [Project].csproj. [Project] will be replaced by your project name.
This will open the project file inside Visual Studio. The project file is an XML file and what you’re looking for is the PropertyGroup section. This defines properties for a particular configuration. In this example there is only Debug and Release. If you had more configurations you’d also see them here.
Enabling compiled views is as easy as adding a new value called MvcBuildViews. This holds a Boolean value that either compiles the view or not. As a best practice I only add this to code I’m releasing into the wild, so Release configurations or similar.
That’s it! Save the file. Right click the project file and choose Reload Project. To witness the changes in action, open a view in your project. Leave the configuration as Debug and create a syntax error. I’ve removed the semi colon from the code block at the top of the page. When I compile the code there are no errors. Changing the build from Debug to Release however produces different results when the code is compiled:
Without enabling this feature, the only way to pick up this syntax error is by running the website. The biggest gain when using this feature is it saves you time. Time that could have been wasted tracking down hard to find bugs in production code.
This isn’t a well known feature of Visual Studio. Hopefully you’ll use this feature and learn to love it as much as I have.
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!