I am currently working on a MVC3 project with Razor. I have switchen on compilation of Views to be aware of spelling errors etc. at compile-time.
As soon as I switch
I have tried a lot of different solutions available in the web, but either they did not quite fit onto my problem, or they did not work.
To recap my problem:
After switching CompileViews on, I immediately got the above error during compile. I am using the default Visual Studio Development Server of VS2010 to test my MVC app.
Today I opened a request at Microsoft Developer support, and - I am almost ashamed to admit it - got my answer approximately 30 seconds into the callback from the technician:
All he said was: Please goto your obj folder and delete all contents. Then compile again.
And that really was all it took.
So after a lot of head-shaking about myself I wanted to share the results with you.
This problem occurs when there is web project output (templated web.config or temporary publish files) in the obj folder. The ASP.NET compiler used isn't smart enough to ignore stuff in the obj folder, so it throws errors instead.
Another fix is to nuke the publish output right before calling <AspNetCompiler>. Open your .csproj and change this:
<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
<AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
</Target>
to this:
<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
<ItemGroup>
<ExtraWebConfigs Include="$(BaseIntermediateOutputPath)\**\web.config" />
<ExtraPackageTmp Include="$([System.IO.Directory]::GetDirectories("$(BaseIntermediateOutputPath)", "PackageTmp", System.IO.SearchOption.AllDirectories))" />
</ItemGroup>
<Delete Files="@(ExtraWebConfigs)" />
<RemoveDir Directories="@(ExtraPackageTmp)" />
<AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
</Target>
That will delete all web.configs under \obj, as well as all PackageTmp folders under \obj.