I like the compiler to catch as many errors as possible before I start debugging or release my application. This also applies to code nuggets in ASP.NET MVC views, especially since these are not easily unit-testable. But views are not validated at compilation, at least not when using the standard settings for ASP.NET MVC projects.
Tools like Resharper can notify you of syntax errors within your views, but you can still build the project and not get a single error. Instead, your application will crash when trying to evaluate the buggy view.
Fortunately you can easily make Visual Studio compile your views along with the rest of your code. The only downside is that your project will build a bit slower, but if this bothers you during development you can also enable this feature on a per configuration basis. For example you may want to compile your views in Release mode, but not in your Debug configuration , so you have the extra safety of compile-time view checking before releasing a new version, but you still get lightning-fast compilation times while developing.
Please proceed as follows to turn on compile time view compilation:
- Open your .csproject or .vbproject file in a text editor of your choice. It’s just a simple xml file, which is why any editor will do fine.
- Locate the <PropertyGroup> element that belongs to the configuration you would like to alter.
- Within this PropertyGroup, add an additional element with “true” as its text value.
For example, this will turn on view compilation in my project’s Release configuration:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <MvcBuildViews>true</MvcBuildViews> ...
That’s it! The next time one of your views has compile-time errors, these will be reported when building your application.
One last thing: You need to repeat this procedure for all configurations you might create in the future if you want your views to be checked in those configurations as well. Even if you tell Visual Studio to copy settings from an existing configuration where this tag is already set to true, the MvcBuildViews tag is not copied by Visual Studio.
Edit: As some readers pointed out, your project’s compilation time will increase after turning on view compilation. For this reason, I have recently switched it off for the debug configuration of my current project. However, compile-time view compilation is still tremendously useful before releasing a new version. Or perhaps even before submitting a new revision to your source control server. It can catch a lot of different errors this way, especially if you are using the new ASP.NET MVC strongly typed Html helpers.
Tags: ASP.NET MVC, MSBuild
Tags: ASP.NET MVC, MSBuild
Adrian is a software developer from Fulda, Germany. Adrian has been programming C++ applications since 1998. Recently he has been implementing a Web 2.0 SaaS website, so his current development-related interests are ASP.NET MVC, C#, and jQuery.
XUpdate mittels XSLT - Ein XUpdate-Prozessor auf XSLT-Basis
May 4th, 2009 at 7:47 pm
ASP.NET MVC Tip: Turn on compile-time View Checking | devermind.com…
DotNetBurner – burning hot .net content…
May 4th, 2009 at 8:17 pm
I made this change in a v1 MVC project, but it doesn’t seem to be working. I adding some bad code: , but no compile time errors. Am I missing something?
May 4th, 2009 at 8:18 pm
Seems my code was stripped out, basically: Html.MyActionLink(), which doesn’t exist.
May 5th, 2009 at 12:11 pm
@Steve: I just tested this again by creating a new project and adding the Element as described above.
My guess is that you might have added the tag to the wrong PropertyGroup element. When you create a new MVC project, there are two configurations, but three different PropertyGroups. I’m not sure what the first one is for, but the second one is for the Debug configuration and the third for Release.
May 5th, 2009 at 3:00 pm
[...] ASP.NET MVC Tip: Turn on compile-time View Checking (Adrian Grigore) [...]
May 7th, 2009 at 8:02 pm
Hey, the first one is general and applies to all builds, both debug and release. I put it in there. What language MVC project are you creating? I’m using C# for the v1 release of .NET MVC.
Thanks!
May 7th, 2009 at 9:33 pm
I am using C# and .NET MVC with Visual Studio 2008 Team System SP1.
Perhaps you could create a new MVC project just to test this, then add the tag, add a typo in a view and then compile. Just to find if it is something that goes wrong in general on your system or something related to your project in particular.
May 7th, 2009 at 9:34 pm
Oh, and I’d also try putting it it one of the other two sections (Debug or Release only) if everything else fails.
June 2nd, 2009 at 7:47 pm
I only recently discovered this the other day and have been working with it for two days now. I hate it! Sure it picks up the view errors on build but it makes the build so so slow. CTRL+SHIFT+B is maybe a 100-a-day operation for me sometimes (coz sometimes just relying on the background thread checking in VS is simply too slow) but with this post-build view checking – it can turn a 3-4second process into a 1-2minute deal. Way too long!
I went back to the ‘old way’ late this afternoon. Sure i may miss some errors in my views and only discover them at runtime.. but i’ve managed that way till now.. and the more i move functionality into helpers the less code there is in the view for problems. It’s kool to know it’s there when you are pulling your hair with a bug you cant find.. or build-completeness.. but for everyday use.. and chewing thru work – its a pain. Sorry, not a fan.
June 2nd, 2009 at 10:23 pm
Matt,
As described in my article, you can add compile-time checking on a per-configuration base only. This way you can keep blazing-fast compilation times in your debug configuration and take advantage of compile-time checking in release mode.
June 7th, 2009 at 9:06 am
[...] Tip: Turn on compile-time View Checking: A nice post from Adrian Grigore who demonstrates how to easily enable compile-time checking of your ASP.NET MVC view files. [...]
June 7th, 2009 at 9:18 am
[...] Tip: Turn on compile-time View Checking: A nice post from Adrian Grigore who demonstrates how to easily enable compile-time checking of your ASP.NET MVC view files. [...]
June 7th, 2009 at 3:21 pm
[...] ASP.NET MVC Tip: Turn on compile-time View Checking | devermind.com [...]
June 7th, 2009 at 8:18 pm
Rather than spend time converting VS2010 to use WPF for the seemingly sole purpose of giving us gradient code highlighting, why didn’t Microsoft spend a bit of time speed-optimizing the website compiler?? I just don’t understand why we’re stuck with this dog of a compiler after THREE major revisions of the IDE!! My hard drive just cranks and cranks and cranks and cranks and cranks … did Microsoft tell the website compiler to bypass the cache explicitly?? I’m at a loss…
June 8th, 2009 at 1:34 pm
[...] ASP.NET MVC Tip: Turn on compile-time View Checking | devermind.com [...]
June 9th, 2009 at 4:51 am
[...] Tip: Turn on compile-time View Checking: Adrian Grigore的一篇好贴,示范如何轻松地启用对ASP.NET MVC视图文件的编译时检查。 [...]
June 11th, 2009 at 2:06 am
[...] der Stelle möchte ich mich bei Daniel Richter für den Tipp danken. Im Web bin ich bereits auf diesen Post gestoßen der dasselbe vorschlägt, falls meine Erklärung nicht verständlich [...]
June 11th, 2009 at 2:41 am
[...] ASP.NET MVC Tip: Turn on compile-time View Checking | devermind.com [...]
June 11th, 2009 at 2:44 am
[...] ASP.NET MVC Tip: Turn on compile-time View Checking | devermind.com [...]
June 11th, 2009 at 3:05 pm
Thanks for the useful info. It’s so interesting
June 12th, 2009 at 4:43 am
I think this is a good website. It give us chance to communicate with the developer face to face ! Like me as a beginner , We need this kind of platform very much. l hope Adrian Grigore can answer questions in time for everyone! Thank you very much!!!!!!!!!
June 12th, 2009 at 10:37 am
Thanks for your kind comments, Javen
. I’ll do my best to answer all questions coming up regarding my articles.
June 14th, 2009 at 6:38 pm
[...] Tip: Turn on compile-time View Checking: A nice post from Adrian Grigore who demonstrates how to easily enable compile-time checking of your ASP.NET MVC view files. [...]
June 14th, 2009 at 11:08 pm
[...] [...]
June 16th, 2009 at 2:53 pm
I also had problems making this work until I added the following code:
June 16th, 2009 at 2:54 pm
http://blog.maartenballiauw.be/post/2009/05/06/More-ASPNET-MVC-Best-Practices.aspx – Compile Views
June 17th, 2009 at 6:28 pm
what we did was to put this as an option in our CI build
we have build views as false in the project file
false
and within the msbuild file which runs within the CI server we turn it back on again
This means we have the check that the views build correctly performed on the build server rather than slowing developers down every time a build is performed.
June 20th, 2009 at 6:35 pm
[...] Truco: Activar la comprovación de la vista en tiempo de compilación: Un post de Adrian Grigore que muestra cómo podemos activar la comprovación en tiempo de compilacion de los archivos de vistas de ASP.NET MVC. [...]
June 28th, 2009 at 1:52 pm
It’s good that this option is available, some might say essential. Thanks for pointing it out. Why is it hidden?
I found a little problem: if the project’s folder name is different to the project’s name then an error occurs when building the project. The error message, raised by the ASPNETCOMPILER, looks like:
The directory ‘<>/<>’ doesn’t exist.
Obviously there’s a work around.
August 9th, 2009 at 2:25 pm
[...] Tip: Turn on compile-time View Checking: A nice post from Adrian Grigore who demonstrates how to easily enable compile-time checking of your ASP.NET MVC view files. [...]
November 16th, 2009 at 5:47 pm
This is very helpful information.
At the starting of project we did many changes to name spaces ,created stronged types views,later changed the type name space etc…We were only able to find these during runtime only.
This tip helped in finding those at compile time..
Good stuff Adrian.
January 11th, 2010 at 7:22 am
[...] expressions when referencing models/viewmodels passed to a view template. This enables better compile-time checking of views (so that bugs can be found at build-time as opposed to runtime), and also enables better code [...]
January 12th, 2010 at 3:58 am
Очень вам признателен, на самом деле полезная информация.
January 17th, 2010 at 7:49 am
[...] 大家对ASP.NET MVC 2要求的一个常用特性是,要我们支持强类型的HTML辅助方法,这样的辅助方法使用 lambda 表达式来引用传到视图模板中的模型或视图模型。这可以促成更好的编译时视图检查(可以在编译时发现缺陷,而不是在运行时),还可以促成视图模板中更好的代码intellisense支持。 [...]
January 17th, 2010 at 8:52 am
[...] expressions when referencing models/viewmodels passed to a view template. This enables better compile-time checking of views (so that bugs can be found at build-time as opposed to runtime), and also enables better code [...]
January 18th, 2010 at 6:44 pm
[...] referenciam modelos/visões de modelo passados para um modelo de visão. Isso permite uma melhor verificação do código das visões no tempo de compilação (de modo que os erros podem ser encontrados em tempo de compilação em oposição ao tempo de [...]
January 18th, 2010 at 6:47 pm
[...] referenciam modelos/visões de modelo passados para um modelo de visão. Isso permite uma melhor verificação do código das visões no tempo de compilação (de modo que os erros podem ser encontrados em tempo de compilação em oposição ao tempo de [...]
January 18th, 2010 at 6:53 pm
[...] referenciam modelos/visões de modelo passados para um modelo de visão. Isso permite uma melhor verificação do código das visões no tempo de compilação (de modo que os erros podem ser encontrados em tempo de compilação em oposição ao tempo de [...]
January 22nd, 2010 at 9:27 pm
[...] expressions when referencing models/viewmodels passed to a view template. This enables better compile-time checking of views (so that bugs can be found at build-time as opposed to runtime), and also enables better code [...]
January 24th, 2010 at 7:56 pm
[...] expresiones lambda cuando referencien a modelos y vistas a una plantilla. Esto permite una mejor revisión en tiempo de compilación de las vistas (de manera que se pueden encontrar errores en tiempo de compilación en lugar de en [...]
February 4th, 2010 at 8:59 pm
[...] expressions when referencing models/viewmodels passed to a view template. This enables better compile-time checking of views (so that bugs can be found at build-time as opposed to runtime), and also enables better code [...]
March 10th, 2010 at 5:42 am
[...] expressions when referencing models/viewmodels passed to a view template. This enables better compile-time checking of views (so that bugs can be found at build-time as opposed to runtime), and also enables better code [...]
April 22nd, 2010 at 1:35 pm
Crap, didn’t realise that would happen
If I remove the li tags then the link works.
May 4th, 2010 at 6:40 am
[...] expressions when referencing models/viewmodels passed to a view template. This enables better compile-time checking of views (so that bugs can be found at build-time as opposed to runtime), and also enables better code [...]
May 20th, 2010 at 11:27 pm
This doesn’t work out of the box so to speak with VS2010, you get an error : 346 It is an error to use a section registered as allowDefinition=’MachineToApplication’ beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS.
June 23rd, 2010 at 3:51 am
[...] developers I talk to prefer to use strongly-typed views. Strong typing gives us intellisense and compiler errors. But – is there a cost? Does strongly typing the view lead to a form of bad coupling between the [...]
June 30th, 2010 at 7:44 pm
[...] [...]
August 30th, 2010 at 9:49 pm
Hi, I am currently working on an application that will be in different languages. One of these in German. when i run the app in en-US, I am able to get the AM or PM for the time using “hh:mm tt”, however when i switch the webconfig file to de-DE for the german information to be applied, I cannot get the AM or PM – I only get the time, eg, 3:04 instead of 3:04 PM.
How do I overcome this challenge?
August 31st, 2010 at 12:02 am
@Roxanne: How is this related to this article?
August 31st, 2010 at 1:17 am
It isn’t really. Just a novice programmer seeking much needed assistance. Forgive me if I was out of place.
August 31st, 2010 at 10:14 am
@Roxanne: Have a look at stackoverflow.com . It’s a fantastic place to freely post questions such as this one and you usually get a reply within a few minutes.
August 31st, 2010 at 9:06 pm
Thank you