Search code examples
asp.netasp.net-mvcvisual-studioweb-configpublish

Visual Studio's "Publish" feature prevents my ASP.NET MVC app from compiling


I've noticed a strange issue when FTP publishing my ASP.NET MVC app in Visual Studio:

After publishing, it won't compile. I get this error:

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.

From my research so far, this error generally occurs when a Web.config file containing certain sections is deeper than root in the project's folder hierarchy.

Well, it turns out I do have some Web.config files deeper in the hierarchy, which I found in these folders:

  • [project folder]\obj\Release\Package\PackageTmp
  • [project folder]\obj\Release\TransformWebConfig\original
  • [project folder]\obj\Release\TransformWebConfig\transformed

The problem is, I didn't put these files in these locations. They get copied there during the publish process (specifically, FTP publish). If I manually delete the Web.configs (or rebuild my solution), I'm once again able to compile. (Well, rebuild usually works--it will fail if I switch from Debug to Release or vice versa after publishing.)

Now, to my questions:

Have I messed up a setting or done something wrong or is this basically just a nuisance that I have to deal with? Should I be concerned? Is this problem fixable?


Solution

  • Are you using <MvcBuildViews>true</MvcBuildViews> in your web project .csproj file? if yes, it could be the problem. Put it to false and the problem is gone.

    Another solution if you really want to use the MvcBuildViews feature is described here: link. I didnt tested it but seems to have good reviews!