devermind.com
Adrian Grigore’s software development weblog. Motto: I will not waste my time looking for a clever motto.
  • Home
  • About
  • Contact me
  • Privacy Policy

ASP.NET MVC Tip #2: Turn on compile-time View Checking

ASP.NET, ASP.NET MVC Add comments

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:

  1. 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.
  2. Locate the <PropertyGroup> element that belongs to the configuration you would like to alter.
  3. 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.

kick it on DotNetKicks.com


Shout it

Tags: ASP.NET MVC, MSBuild


May 3rd, 2009 |

Tags: ASP.NET MVC, MSBuild


52 Responses to “ASP.NET MVC Tip #2: Turn on compile-time View Checking”

  1. DotNetBurner - ASP.net MVC
    May 4th, 2009 at 7:47 pm

    ASP.NET MVC Tip: Turn on compile-time View Checking | devermind.com…

    DotNetBurner – burning hot .net content…


  2. Steve Andrews
    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?


  3. Steve Andrews
    May 4th, 2009 at 8:18 pm

    Seems my code was stripped out, basically: Html.MyActionLink(), which doesn’t exist.


  4. Adrian Grigore
    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.


  5. Dew Drop - May 5, 2009 | Alvin Ashcraft's Morning Dew
    May 5th, 2009 at 3:00 pm

    [...] ASP.NET MVC Tip: Turn on compile-time View Checking (Adrian Grigore) [...]


  6. Steve Andrews
    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!


  7. Adrian Grigore
    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.


  8. Adrian Grigore
    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.


  9. matt kocaj
    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. :(


  10. Adrian Grigore
    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.


  11. June 7th Links: ASP.NET, AJAX, ASP.NET MVC, Visual Studio - ScottGu's Blog
    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. [...]


  12. BusinessRx Reading List : June 7th Links: ASP.NET, AJAX, ASP.NET MVC, Visual Studio
    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. [...]


  13. Daily Links for Sunday, June 7th, 2009 | LaptopHeaven's Blog
    June 7th, 2009 at 3:21 pm

    [...] ASP.NET MVC Tip: Turn on compile-time View Checking | devermind.com [...]


  14. Mike
    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…


  15. Daily Links for Monday, June 8th, 2009 | LaptopHeaven's Blog
    June 8th, 2009 at 1:34 pm

    [...] ASP.NET MVC Tip: Turn on compile-time View Checking | devermind.com [...]


  16. 6月7日链接篇:ASP.NET, AJAX, ASP.NET MVC, Visual Studio-Scott Guthrie 博客中文版
    June 9th, 2009 at 4:51 am

    [...] Tip: Turn on compile-time View Checking: Adrian Grigore的一篇好贴,示范如何轻松地启用对ASP.NET MVC视图文件的编译时检查。 [...]


  17. HowTo: ASP.NET MVC View compile-time checkin | Code-Inside Blog
    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 [...]


  18. Daily Links for Sunday, June 7th, 2009
    June 11th, 2009 at 2:41 am

    [...] ASP.NET MVC Tip: Turn on compile-time View Checking | devermind.com [...]


  19. Daily Links for Monday, June 8th, 2009
    June 11th, 2009 at 2:44 am

    [...] ASP.NET MVC Tip: Turn on compile-time View Checking | devermind.com [...]


  20. JamesD
    June 11th, 2009 at 3:05 pm

    Thanks for the useful info. It’s so interesting


  21. Javen
    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!!!!!!!!!


  22. Adrian Grigore
    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.


  23. June 7th Links: ASP.NET, AJAX, ASP.NET MVC, Visual Studio | rapid-DEV.net
    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. [...]


  24. Блог Скотта Гатри (перевод) : Ссылки на 7-е июня: ASP.NET, AJAX, ASP.NET MVC, Visual Studio
    June 14th, 2009 at 11:08 pm

    [...] [...]


  25. Graham
    June 16th, 2009 at 2:53 pm

    I also had problems making this work until I added the following code:


  26. Graham
    June 16th, 2009 at 2:54 pm

    http://blog.maartenballiauw.be/post/2009/05/06/More-ASPNET-MVC-Best-Practices.aspx – Compile Views


  27. Richard
    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.


  28. Enlaces de Junio: ASP.NET, AJAX, ASP.NET MVC, Visual Studio « Thinking in .NET
    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. [...]


  29. Paul
    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.


  30. Some Useful Links For .NET | KinnarShah.in | Blog
    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. [...]


  31. Tiru
    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.


  32. ASP.NET MVC 2: Strongly Typed Html Helpers - ScottGu's Blog
    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 [...]


  33. Семен Соколов
    January 12th, 2010 at 3:58 am

    Очень вам признателен, на самом деле полезная информация.


  34. ASP.NET MVC 2博客系列之一:强类型HTML辅助方法-Scott Guthrie 博客中文版
    January 17th, 2010 at 7:49 am

    [...] 大家对ASP.NET MVC 2要求的一个常用特性是,要我们支持强类型的HTML辅助方法,这样的辅助方法使用 lambda 表达式来引用传到视图模板中的模型或视图模型。这可以促成更好的编译时视图检查(可以在编译时发现缺陷,而不是在运行时),还可以促成视图模板中更好的代码intellisense支持。 [...]


  35. ASP.NET MVC 2: Strongly Typed Html Helpers | newsgone
    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 [...]


  36. ASP.NET MVC 2: Métodos de Ajuda Fortemente Tipados - ScottGu's Blog em Português
    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 [...]


  37. ASP.NET MVC 2: Métodos de Ajuda Fortemente Tipados - ScottGu's Blog em Português
    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 [...]


  38. ASP.NET MVC 2: Métodos de Ajuda HTML Fortemente Tipados - ScottGu's Blog em Português
    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 [...]


  39. BusinessRx Reading List : ASP.NET MVC 2: Strongly Typed Html Helpers
    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 [...]


  40. ASP.NET MVC: Html Helpers Fuertemente Tipados « Thinking in .NET
    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 [...]


  41. ASP.NET MVC 2: Strongly Typed Html Helpers « Turgay Ceylan
    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 [...]


  42. ASP.NET MVC 2: Strongly Typed Html Helpers ­ Share My Live
    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 [...]


  43. sohbet
    April 22nd, 2010 at 1:35 pm

    Crap, didn’t realise that would happen :-)
    If I remove the li tags then the link works.


  44. ASP.NET MVC 2: Strongly Typed Html Helpers  | OOP – Object Oriented Programing
    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 [...]


  45. Brandon
    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.


  46. Parr's Rules for MVC Views
    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 [...]


  47. Ссылки на 7-е июня: ASP.NET, AJAX, ASP.NET MVC, Visual Studio - Aggregated Russian Blogs - Site Home - MSDN Blogs
    June 30th, 2010 at 7:44 pm

    [...] [...]


  48. Roxanne
    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?


  49. Adrian Grigore
    August 31st, 2010 at 12:02 am

    @Roxanne: How is this related to this article?


  50. Roxanne
    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.


  51. Adrian Grigore
    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.


  52. Roxanne
    August 31st, 2010 at 9:06 pm

    Thank you


Leave a Reply

  • Feeds

    • RSS feed iconAll Entries
    • RSS feed iconAll Comments
    • RSS feed iconThis Post's Comments
  • About Adrian Grigore

    Adrian Grigore 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.


  • Adrian's (German language) book

    XSLT XUpdate BuchXUpdate mittels XSLT - Ein XUpdate-Prozessor auf XSLT-Basis


  • Pages

    • About
    • Contact me
    • Privacy Policy
  • Links

    • Lobstersoft
    • SonicWeasel
  • Tags

    ASP.NET ASP.NET MVC binding Business client-side form validation client-side validation codedui selenium testing test CSharp custom DAL dataannotationsmodelbinder form validation games guide jQuery jQuery.validate LINQ MSBuild multi-tier mvc POST remote validation shareware tutorial viewmodel visual-studio asp.net xVal
  • Archive

    • May 2010 (2)
    • January 2010 (2)
    • June 2009 (1)
    • May 2009 (1)
    • April 2009 (1)
    • March 2009 (2)
    • February 2009 (1)
    • October 2008 (2)
Copyright © 2010 devermind.com All Rights Reserved
RSS XHTML CSS Log in
Wp Theme by n Graphic Design
Powered by Wordpress