By John Oxley

2008-12-20 10:51:00 8 Comments

I want an msbuild task to compile the views so I can see if there are compile time errors at well... compile time. Any ideas?


@Mirko 2013-04-10 21:26:54

I frankly would recommend the RazorGenerator nuget package. That way your views have a .designer.cs file generated when you save them and on top of getting compile time errors for you views, they are also precompiled into the assembly (= faster warmup) and Resharper provides some additional help as well.

To use this include the RazorGenerator nuget package in you ASP.NET MVC project and install the "Razor Generator" extension under item under Tools → Extensions and Updates.

We use this and the overhead per compile with this approach is much less. On top of this I would probably recommend .NET Demon by RedGate which further reduces compile time impact substantially.

Hope this helps.

@daniel 2013-05-07 12:22:42

is there a similiar solution vor VS2012?

@Ed DeGagne 2013-07-26 14:59:13

Unfortunate that it only supports C# and no VB.Net

@Jeroen K 2014-08-01 09:19:46

@zoidbergi RazorGenerator works with VS2012; when using RazorGenerator.Mvc and RazorGenerator.MsBuild: no need for extension. See blog entry on

@niico 2017-11-07 23:57:08

Can this be used to only find errors - or does it replace the view engine when deploying the application?

@Michael Samteladze 2018-12-19 06:34:07

I've installed RazorGenerator nuget package and Razor Generator extension. Nothing has been changed in my project. No .designer.cs files appeared. I use visual studio 2017.

@Mirko 2018-12-20 12:34:11

@MichaelSamteladze Haven't tested with .NET Core and haven't done Razor in ages, but assuming you are on full framework take a look at the Razor Generator docs here: Easiest is to use the Enable-RazorGenerator in the Package Manager Console. Good Luck!

@Mirko 2018-12-20 12:51:20

@MichaelSamteladze I tried (caveat with 1 minute of investment) on core with no luck (on either pages or views) and I am not sure I would advise anyone to use ASP.NET MVC (meaning non-.Core) today if they intended to use Razor pages or views and for the app to live for a while (if greenfield) unless there is a compelling argument against Core. No idea what applies to your case.

@Michael Samteladze 2018-12-21 09:01:31

@Mirko my project is not .NET Core, I have regular MVC5 Web Application

@maxnk 2008-12-20 11:04:12

You can use aspnet_compiler for this:

C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_compiler -v /Virtual/Application/Path/Or/Path/In/IIS/Metabase -p C:\Path\To\Your\WebProject -f -errorstack C:\Where\To\Put\Compiled\Site

where "/Virtual/Application/Path/Or/Path/In/IIS/Metabase" is something like this: "/MyApp" or "/lm/w3svc2/1/root/"

Also there is a AspNetCompiler Task on MSDN, showing how to integrate aspnet_compiler with MSBuild:

<Project xmlns="">
    <Target Name="PrecompileWeb">

@JarrettV 2009-02-12 20:00:34

This is out of date, see an excerpt from the readme doc below.

@mlhDev 2019-09-27 20:22:45

The other answer describes the project task in more detail, but the aspnet_compiler portion is still correct (and useful on build agents).

@Veverke 2015-03-31 10:49:14

Using Visual Studio's Productivity Power Tools (free) extension helps a bit. Specifically, the Solution Error Visualizer feature. With it, compilation errors marked visually in the solution explorer (in the source file where the error was found). For some reason, however, this feature does not work as with other errors anywhere else in the code.

With MVC views, any compile-time errors will still be underlined in red in their respective .cs files, but signaling these errors is not propagated upwards in the Solution Explorer (in no way, even not in the containing source file).

Thanks for BlueClouds for correcting my previous statement.

I have just reported this as an issue on the extension's github project.

@Blue Clouds 2017-02-09 15:19:05

I tried Productivity Power Tools. But does not behave as what is said here. There is error in razor view but build is success. views are not marked or underlined in red, or anywhere in solution exploer tree.

@Veverke 2017-02-12 07:43:46

@BlueClouds: you are right. I created a sample project and added a compile-time error in a view. The extension will underline the erroring lines in red, but will not propagate the error up in the solution explorer. Am correcting what I stated in the answer. Am leaving the answer here as it still helps a bit, while indeed not solving the problem effectively.

@Abu Abdullah 2016-11-03 08:25:10

Build > Run Code Analysis

Hotkey : Alt+F11

Helped me catch Razor errors.

@Michael12345 2017-08-17 02:07:35

I upvoted this answer because the hotkey did indeed expose a Razor error. However I subsequently noticed that it only seems to work if you have the .cshtml file open in the IDE.

@JarrettV 2009-02-12 19:52:21

From the readme word doc for RC1 (not indexed by google)

ASP.NET Compiler Post-Build Step

Currently, errors within a view file are not detected until run time. To let you detect these errors at compile time, ASP.NET MVC projects now include an MvcBuildViews property, which is disabled by default. To enable this property, open the project file and set the MvcBuildViews property to true, as shown in the following example:

<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="">

Note Enabling this feature adds some overhead to the build time.

You can update projects that were created with previous releases of MVC to include build-time validation of views by performing the following steps:

  1. Open the project file in a text editor.
  2. Add the following element under the top-most <PropertyGroup> element: <MvcBuildViews>true</MvcBuildViews>
  3. At the end of the project file, uncomment the <Target Name="AfterBuild"> element and modify it to match the following:
<Target Name="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
    <AspNetCompiler VirtualPath="temp" PhysicalPath="$(ProjectDir)\..\$(ProjectName)" />

@Adrian Grigore 2009-04-20 16:59:13

If this should not work for your project, check if there isn't an <MvcBuildViews>false</MvcBuildViews> somewhere in your project file. It was overriding the new <MvcBuildViews> element I added on top of it.

@Zhaph - Ben Duguid 2009-07-28 20:06:54

@mxmissile: Scott Guthrie recommended adding a Web Deployment Project to your solution to get this sort of support in Web Application Projects:…

@Carl Hörberg 2010-04-14 08:41:09

Make sure that EnableUpdateable is set to false or else the views wont be precompiled. <EnableUpdateable>false</EnableUpdateable> <MvcBuildViews>true</MvcBuildViews> (…)

@DevelopingChris 2010-11-24 17:25:02

I'm having an issue with this, where a default namespace is imported in the web.config, but the pages are failing to compile, any hints?

@dariol 2011-07-11 07:50:21

Why, why, why...there is no keyboard shortcut for building with or without views? MS why?

@lko 2014-05-07 08:06:40

This is the solution added to the MVC tools.

@Jenny O'Reilly 2015-05-16 10:28:41

For me this solution displays errors in the generated .cs files, rather than the actual errors in the .cshtml files. Is there a way to change this? It's not really "usable" this way.

@niico 2017-11-07 21:54:20

It works but double clicking the error in the error list doesn't open the View - anyway to do this?

@Austin Heller 2019-05-02 19:55:16

After applying the solution above, I started getting an error relating to my web.config (due to a release version existing in my obj folder). To fix that, you can remove the obj folders during the BeforeBuild phase. Source:…

@user1040323 2011-11-17 15:04:17

The answer given here works for some MVC versions but not for others.

The simple solution worked for MVC1 but on upgrading to MVC2 the views were no longer being compliled. This was due to a bug in the website project files. See this Haacked article.

See this:

@bh213 2008-12-20 12:25:29

Also, if you use Resharper, you can active Solution Wide Analysis and it will detect any compiler errors you might have in aspx files. That is what we do...

@mookid8000 2009-01-11 11:12:17

It's true it works for aspx files, but the solution-wide analysis does not include ascx files (user controls)

@Andrew 2010-07-24 13:47:36

I believe it does in R# 5, but it's a huge resource hog for large projects (even on my 16GB home machine it's not worth using).

@Drew Noakes 2010-09-02 01:08:39

@Andrew / @mookid8000 -- R# will also catch errors that the compiler won't, such as missing/incorrect views and actions. R# will slow your PC down a bit (I find it fine on a large-ish project with 4GB ram and a hyperthreaded CPU) but I easily make back the time I spend waiting for it, and I end up doing fewer operations on my code as R# provides higher level operations that batch together the many steps I'd have to take to achieve the same task manually. Your project must be huge!

@Beep beep 2011-06-26 00:53:37

For large projects, "slow your PC down a bit" is an understatement. My build machine has 16GB of RAM and 8 cores (2 Xeons), and it just CRAWLS. I have a feeling R# just wasn't made for projects our size though ... e.g. our solution has ~30 projects, a couple Million LOC, and many hundreds of views. I love R# on our smaller projects (e.g. a few projects and no more than 50 views), but on our big one we always have to turn it off.

@emragins 2014-03-10 22:15:11

This MAY work, but RUN AWAY! I turned this on thinking my solution was small and it never finished 'analyzing' and ate all my RAM and CPU. Took me 15 minutes to recover.

@bh213 2008-12-20 12:12:35

Next release of ASP.NET MVC (available in January or so) should have MSBuild task that compiles views, so you might want to wait.

See announcement

Related Questions

Sponsored Content

44 Answered Questions

18 Answered Questions

[SOLVED] Can an ASP.NET MVC controller return an Image?

52 Answered Questions

[SOLVED] Could not find a part of the path ... bin\roslyn\csc.exe

16 Answered Questions

[SOLVED] How to get all Errors from ASP.Net MVC modelState?

7 Answered Questions

[SOLVED] Display a view from another controller in ASP.NET MVC

21 Answered Questions

[SOLVED] File Upload ASP.NET MVC 3.0

15 Answered Questions

[SOLVED] How to render an ASP.NET MVC view as a string?

36 Answered Questions

[SOLVED] How do you create a dropdownlist from an enum in ASP.NET MVC?

9 Answered Questions

8 Answered Questions

[SOLVED] How to get ELMAH to work with ASP.NET MVC [HandleError] attribute?

Sponsored Content