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?
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
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:
- Open the project file in a text editor.
- Add the following element under the top-most
- 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>
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.
You can use aspnet_compiler for this:
C:WindowsMicrosoft.NETFrameworkv2.0.50727aspnet_compiler -v /Virtual/Application/Path/Or/Path/In/IIS/Metabase -p C:PathToYourWebProject -f -errorstack C:WhereToPutCompiledSite
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:inetpubwwwrootMyWebSite" TargetPath="c:precompiledwebMyWebSite" Force="true" Debug="true" /> </Target> </Project>
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…
Next release of ASP.NET MVC (available in January or so) should have MSBuild task that compiles views, so you might want to wait.
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.
Build > Run Code Analysis
Hotkey : Alt+F11
Helped me catch Razor errors.
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).
BlueClouds for correcting my previous statement.
I have just reported this as an issue on the extension’s github project.