589

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?

1
  • 1
    I don't know what viewengine you're using, but if you're using Razor, you might want to check out my blog post: <a href="chrisvandesteeg.nl/2010/11/22/… your asp.net mvc Razor views into a seperate dll</a> Should be possible to use that code for other viewengines as well, but haven't done & tested that yet Nov 23, 2010 at 6:26

9 Answers 9

592

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="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <MvcBuildViews>true</MvcBuildViews>
  </PropertyGroup>

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)" />
</Target>
9
  • 29
    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. Apr 20, 2009 at 16:59
  • 3
    @mxmissile: Scott Guthrie recommended adding a Web Deployment Project to your solution to get this sort of support in Web Application Projects: weblogs.asp.net/scottgu/archive/2006/09/22/… Jul 28, 2009 at 20:06
  • 6
    Make sure that EnableUpdateable is set to false or else the views wont be precompiled. <EnableUpdateable>false</EnableUpdateable> <MvcBuildViews>true</MvcBuildViews> (devcarl.posterous.com/…) Apr 14, 2010 at 8:41
  • 62
    Why, why, why...there is no keyboard shortcut for building with or without views? MS why?
    – dariol
    Jul 11, 2011 at 7:50
  • 2
    This is the solution added to the MVC tools. stackoverflow.com/a/2670792/878612
    – lko
    May 7, 2014 at 8:06
161

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.

8
  • 1
    is there a similiar solution vor VS2012?
    – daniel
    May 7, 2013 at 12:22
  • 7
    Unfortunate that it only supports C# and no VB.Net
    – Ed DeGagne
    Jul 26, 2013 at 14:59
  • 4
    @zoidbergi RazorGenerator works with VS2012; when using RazorGenerator.Mvc and RazorGenerator.MsBuild: no need for extension. See blog entry on stacktoheap.com
    – Jeroen K
    Aug 1, 2014 at 9:19
  • 2
    Can this be used to only find errors - or does it replace the view engine when deploying the application?
    – niico
    Nov 7, 2017 at 23:57
  • 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. Dec 19, 2018 at 6:34
54

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="http://schemas.microsoft.com/developer/msbuild/2003">
    <Target Name="PrecompileWeb">
        <AspNetCompiler
            VirtualPath="/MyWebSite"
            PhysicalPath="c:\inetpub\wwwroot\MyWebSite\"
            TargetPath="c:\precompiledweb\MyWebSite\"
            Force="true"
            Debug="true"
        />
    </Target>
</Project>
2
  • 5
    This is out of date, see an excerpt from the readme doc below.
    – JarrettV
    Feb 12, 2009 at 20:00
  • 1
    The other answer describes the project task in more detail, but the aspnet_compiler portion is still correct (and useful on build agents).
    – mlhDev
    Sep 27, 2019 at 20:22
28

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

5
  • 4
    It's true it works for aspx files, but the solution-wide analysis does not include ascx files (user controls)
    – mookid8000
    Jan 11, 2009 at 11:12
  • 3
    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).
    – Andrew
    Jul 24, 2010 at 13:47
  • 3
    @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! Sep 2, 2010 at 1:08
  • 5
    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.
    – Beep beep
    Jun 26, 2011 at 0:53
  • 1
    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.
    – emragins
    Mar 10, 2014 at 22:15
15

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

0
9

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: http://haacked.com/archive/2011/05/09/compiling-mvc-views-in-a-build-environment.aspx

0
8

Build > Run Code Analysis

Hotkey : Alt+F11

Helped me catch Razor errors.

1
  • 2
    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. Aug 17, 2017 at 2:07
3

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.

2
  • 1
    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. Feb 9, 2017 at 15:19
  • 2
    @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.
    – Veverke
    Feb 12, 2017 at 7:43
0

If you have multiple web projects with mvc views in a solution and a lot of mvc views, building them can double the total build time. At lest this happend to me.

MSBuild has a feature called incremental builds that can be used to tell msbuild to avoid building views again if there are no changes.

The quote from the documentation says:

To enable incremental builds (builds in which only those targets that have not been built before or targets that are out of date, are rebuilt), the Microsoft Build Engine (MSBuild) can compare the timestamps of the input files with the timestamps of the output files and determine whether to skip, build, or partially rebuild a target.

We just need to provide Inputs and Outputs. As inputs we can use our views, and for output we can generate an empty file somewhere in the obj folder.

We also need to remember to cleanup the output file when Cleanup is run.

Following code can do that:

<PropertyGroup>
  <BuildDependsOn>
    $(BuildDependsOn);
    BuildMvcViews;
  </BuildDependsOn>

  <BuildMvcViewsCacheFile>$(IntermediateOutputPath)$(MSBuildProjectFile).BuildMvcViews.cache</BuildMvcViewsCacheFile>
</PropertyGroup>

<ItemGroup>
  <BuildMvcViewsInputs Include="$(ProjectDir)Views\**\*.cshtml"></BuildMvcViewsInputs>
</ItemGroup>

<Target Name="BuildMvcViews" Inputs="@(BuildMvcViewsInputs)" Outputs="$(BuildMvcViewsCacheFile)">
  <AspNetCompiler VirtualPath="temp" PhysicalPath="$(ProjectDir)" Condition="'@(BuildMvcViewsInputs)' != ''"/>
  <WriteLinesToFile
    File="$(BuildMvcViewsCacheFile)"
    Lines=""
    Overwrite="true"
    Condition="'@(BuildMvcViewsInputs)' != ''"/>
</Target>

<PropertyGroup>
  <CleanDependsOn>
    $(CleanDependsOn);
    CleanBuildMvcViewsCacheFile;
  </CleanDependsOn>
</PropertyGroup>

<Target Name="CleanBuildMvcViewsCacheFile">
  <Delete Files="$(BuildMvcViewsCacheFile)" />
</Target>

More info on my blog post: https://www.bartlomiejmucha.com/en/blog/msbuild/how-to-optimize-build-times-for-mvc-views

Not the answer you're looking for? Browse other questions tagged or ask your own question.