How to remove bin and/or obj folder before the build or deploy
In one of my previous articles, I explained How to: Turn on MVC compiled views or Turning on views compilation which can be very useful to detect potential errors in views code.
However, once I’ve converted my projects to Visual Studio 2013 I star receiving error on build or deploy:
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.
There is a lot of information in Internet why it’s happening and how to resolve the problem. Basically, before the build/deploy, we have to cleanup (or remove) obj folder for web application project(s) – project with enabled compiled views.
I think I found simple, elegant way how to remove obj folder before the build. For those who knows how to edit project file – just add <RemoveDir Directories="$(BaseIntermediateOutputPath)" /> command in BeforeBuild target. Yes, BaseIntermediateOutputPath – variable which keeps obj folder path by default.
For others – here is how to do it step-by-step.
Unload project. Right click on project name in Solution Explorer and select Unload Project:
Right click again on project name and select Edit:
You should see project file in XML format. In the bottom find and uncomment BeforeBuild (and AfterBuild if you want) targets. If you don’t have it – you can just add it. And to BeforeBuild target add <RemoveDir Directories="$(BaseIntermediateOutputPath)" /> command (If you want also delete bin folder add <RemoveDir Directories="$(BaseOutputPath)" /> commend). So your target should look like this:
<Target Name="BeforeBuild"> <!-- Remove obj folder --> <RemoveDir Directories="$(BaseIntermediateOutputPath)" /> <!-- Remove bin folder --> <RemoveDir Directories="$(BaseOutputPath)" /> </Target>
Save and Reload the project:
Now try to build or deploy couple of times – you should not have any more this weird error.
Join the discussion