Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3 Strange error after switching on compilation of views

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 on the <MvcBuildViews>true</MvcBuildViews> in the projects configuration file a get the following error during compile:

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

I read several possible solutions to the problem, mostly concerning IIS and virtual Directories or Applications. The problem is, that I do not use IIS, but instead use the default Visual Studio Development Server.

What can I do to solve this problem?

like image 302
nttakr Avatar asked Mar 01 '11 23:03

nttakr


2 Answers

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.

like image 162
nttakr Avatar answered Nov 07 '22 20:11

nttakr


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(&quot;$(BaseIntermediateOutputPath)&quot;, &quot;PackageTmp&quot;, 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.

like image 21
Chris Hynes Avatar answered Nov 07 '22 21:11

Chris Hynes