Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3 project build fails because of web.config in obj folder

I have <MvcBuildViews>true</MvcBuildViews> setup in my project files.

Now when I do deployment the first time I get files in obj\release\package\packagetmp. Every subsequent build after this results in a faild build.

Web -> C:\Projects\ProjectX\Web\bin\ProjectX.Web.dll
C:\Projects\ProjectX\Web\obj\release\package\packagetmp\web.config(64): 
error ASPCONFIG: 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.
------ Skipped Publish: Project Web, Configuration: Release Any CPU ------

Now if delete the obj folder, I can build fine.

This is rather frustrating to have any build fail after I publish until I manually delete the obj folder. Is there anything I can do to fix this?

like image 787
Chris Marisic Avatar asked Sep 12 '11 13:09

Chris Marisic


2 Answers

Add this to the .csproj file:

<Target Name="AfterBuild" Condition="'$(Configuration)'=='Release'">
  <RemoveDir Directories="$(IntermediateOutputPath)" />
</Target>

Seems to delete the files in obj\Release but not the folder itself, at least on my machine.

like image 91
kendaleiv Avatar answered Oct 18 '22 04:10

kendaleiv


This problem occurs because MvcBuildViews conflicts with Web Deploy packaging. I got the idea for this solution from http://www.zvolkov.com/clog/2011/02/16/asp-net-razor-lessons-learned/ :

  <PropertyGroup>
    <PackageDependsOn Condition="'$(DeployOnBuild)'=='true'">
      CleanWebsitesPackage;
      CleanWebsitesPackageTempDir;
      CleanWebsitesTransformParametersFiles;
      MvcBuildViews;
      $(PackageDependsOn)
    </PackageDependsOn>
    <BuildDependsOn Condition="'$(DeployOnBuild)'!='true'">
      $(BuildDependsOn);
      MvcBuildViews
    </BuildDependsOn>
  </PropertyGroup>

  <Target Name="MvcBuildViews" Condition="'$(MvcBuildViews)'=='true'">
    <AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
  </Target>

Now AspNetCompiler will run before packaging. This works nicely with the msbuild /p:DeployOnBuild=True /p:DeployTarget=Package approach.

like image 25
candrews Avatar answered Oct 18 '22 03:10

candrews