216

It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level.

The top line in all of my aspx pages in my /portal/ directory has this error message, and I know it's a common one. I have googled this error message to no end, and I see a lot of posts telling me to configure the /portal/ folder as an application in IIS (which I have), and more posts telling me I have nested web.configs (but none of the postings offer guidance toward a solution).

My setup is that I have a web.config in my root directory, and then I'm trying to make a company portal, in the /portal/ directory. The /portal/ directory has its own (necessary) web.config.

My web.config line 50 is like this:

<customErrors mode="Off" defaultRedirect="customerrorpage.aspx"/>
<anonymousIdentification enabled="true"/>
<authentication mode="Forms"/>
<membership defaultProvider="MyProvider">

So I have domain.example/web.config and domain.example/portal/web.config ... so my domain.example/portal/default.aspx page will not load.

What is the real solution to this? Do I somehow find a way to merge my root web.config with my /portal/ directory web.config, or am I way off base here?

3
  • 1
    If you are expecting /portal/ to be a separate application under your main website (which you are if you are dropping a web.config in there) then you do need to set it up as a virtual directory. Which version of IIS are you using? You can usually right click the directory and, under Properties, look for the "Directory" tab and hit the "Create" button next to application name, which is probably currently greyed out. To limit the scope of the parent web.config file, look at the attribute inheritInChildApplications="false". Let me know your IIS version.
    – dash
    Feb 15, 2012 at 20:58
  • Hi Dash. I'm using IIS7, and the /portal/ is just supposed to be an employee directory for our company where people can store documents, keep a calendar, and things like that. I think Benni kinda summed up what you were saying -- in his second option, he outlined the IIS solution. I'm sure between one of those two solutions, I can figure this out. Thanks for your time in reading this Dash, and helping me out. Once I see that "create" option, I'll know I'm on my way! Thanks again! Feb 15, 2012 at 21:55
  • 3
    I had this problem, after I defined the publish profile to precompile before publishing. so the obj folder hat a web.config which was breaking the project, after deleting everything in the obj folder, it worked again.
    – Rumplin
    Feb 25, 2019 at 9:33

31 Answers 31

229

Just for background information; Configuration information for an ASP.NET website is defined in one or more Web.config files. The configuration settings are applied in a hierarchical manner. There's a “global” Web.config file that spells out the baseline configuration information for all websites on the web server; this file lives in the %WINDIR%\Microsoft.Net\Framework\version\CONFIG folder. You can also have a Web.config file in the root folder of your website. This Web.config file can override settings defined in the “global” Web.config file, or add new ones. Additionally, you may have Web.config files in the subfolders of your website, which define new configuration settings or override configuration settings defined in Web.config files higher up in the hierarchy.

Certain configuration elements in Web.config cannot be defined beyond the application level, meaning that they must be defined in the “global” Web.config file or in the Web.config file in the website's root folder. The <authentication> element is one such example. The above error message indicates that there is a Web.config file in one of the website's subfolders that has one of these configuration elements that cannot be defined beyond the application level.

Source: http://scottonwriting.net/sowblog/archive/2010/02/17/163375.aspx

You have correctly identified the 2 possible approaches.

1 - Depending on the contents of your second web.config and if your setup would allow (i.e same authentication method) - add the <authentication> settings and any other elements that should be define globally into the top web.config

2 - If you cannot merge then web.config contents then you should be able to turn the sub-folder into a web application in IIS by following the steps contained in this link archived link below. The original link is no longer working. (see archived)

10
  • 8
    Yes, Benni, absolutely that helped and cleared things up -- a lot of things up. I truly appreciate your time in answering this question; I learned a ton just by reading your answer, and things make a little more sense now. I think I'm going to attempt to merge the two web.config files -- at least the authentication part -- and if that doesn't work, I'll look into the various IIS7 options. Thanks again for your time and the information, Benni! Feb 15, 2012 at 21:53
  • 14
    When I build a web deployment package, it causes this problem the next time I build. It has a copy of Web.config in MyWebSiteProject/obj/Debug/... Apr 30, 2013 at 2:06
  • 4
    After nearly a day of trying to figure this one out, finally a descriptive in-depth answer that actually explained the issue, as opposed to saying... just clean your solution and rebuild. Thanks for this! Apr 30, 2013 at 7:59
  • 3
    I found the biggest issue here was that an initial build worked, but then a subsequent build failed because the settings that were allowed in the top-level web.config were not legal in the web.config that was now copied to the obj folder. The trick was to remove the offending attributes on the top level and remove the obj folder to get a legal web.config into a sub-folder. Obviously this is a bug in Visual Studio, but the work-around is straight-forward if you don't need to use attributes in the top-level web.config that are illegal in the sub-level web.config.
    – csells
    Jul 8, 2014 at 19:19
  • 3
    In Visual Studio 2012 and 2013, when the config file has Copy to Output Directory = Always Copy, the Publish process seems to drop copies of the config file into bin and obj folders, which then triggers this exception. The solution for this Publish problem, identified by Tim C., is to set the config file property to Do Not Copy. Oct 24, 2016 at 14:41
88

For what it's worth, I had received the error, "It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level." and ended up resolving it by clearing the \myWebApp\obj\Debug and \myWebApp\obj\Release directories. I also needed to set a default startup page. But, then the app then started up fine. HTH.

6
  • 28
    +1 Deleting the OBJ folder seems to be a common fix... stackoverflow.com/a/5175074/188926
    – Dunc
    Jan 20, 2014 at 16:08
  • 2
    Deleting contents of the OBJ folder fixed this for me too. There seems to be a lot of other potential causes to this error. The phrase which helped me find the right answer was "while publishing".
    – Jim Neff
    Oct 24, 2014 at 12:51
  • 1
    If I had on OBJ folder, I would try that. Dec 9, 2016 at 23:05
  • Setting the default startup page did the trick for me after deleting the Debug folder wasn't enough (so be sure to do both).
    – LoJo
    Apr 2, 2018 at 23:48
  • Tried this and it didn't work. Then i realized I forgot to convert my published files to an application within IIS.
    – eaglei22
    Mar 4, 2020 at 20:19
61

As RY4N says above, it's not necessarily the web.config in your Project folder that causes the problem. In some cases I have found that running a build under the Debug profile will leave behind detritus in the Debug folder under the project in question. There is often a web.config file in here that leads to the error above when you subsequently run a build under the Release profile.

The solution that works for me here is to delete the entire Debug folder that the prior build(s) created under the project directory.

2
  • 1
    Thank you Matthew; I finally figured it out awhile ago, but yes, I had to compile in release, not debug. Appreciate you taking the time to respond! Nov 21, 2012 at 21:49
  • 3
    Thanks for the tip! I just deleted the bin and obj folders in my website project and it solved the issue when I rebuilt.
    – Paul Stark
    Dec 10, 2014 at 21:22
24

It was also happening on my home computer but ONLY when I enabled Build Views on the release configuration AND built a Release configuration. Otherwise it didn't happen.

Though the Build Views option is very nice I ended up disabling it because this "error" would always pop up and let me unable to run the app.

4
  • 2
    Thank you Lord ... that seemed to do the trick, along with a few other suggestions. I appreciate you taking the time to answer! Nov 21, 2012 at 23:27
  • 1
    This was happening to me as well. Not sure why. It sure as heck makes the "MvcBuildViews" setting a whole lot less useful.
    – Ken Smith
    Oct 20, 2013 at 6:09
  • Is there a way to have MvcBuildViews set only for the user settings? LIke in the webproject.csproj.user file?
    – C. Tewalt
    Jun 6, 2014 at 14:28
  • I started getting this error after installing Update 2 for VS 2013. I have a project that has a top level and a nested child web site, both are configured as IIS applications. Turning off MvcBuildViews solved the problem for me as well. Jun 19, 2014 at 14:11
13

Just to say

If you Upgrade(eg 2008 -> 2010) A project Visual studio will create a backup (if you allow it) in the project solution which is added to the new solution, The old Webconfig is where the error pointed out above can then emanate from.

"Web.config file in one of the website's subfolders than has one of these configuration elements that cannot be defined beyond the application level."@benni_mac_b

To Fix it: Just remove the backup folder from the project and solution, in this scenario.

1
  • Thank you for that information, Ryan .... I only have VS 2010. I'm not quite sure what you mean when you say remove the backup folder from the project and solution. But I'll look further into your answer. Again, thank you for taking the time to try to help me out! I just posted a follow-up question here: stackoverflow.com/questions/10414751/… May 2, 2012 at 13:31
13

I have come up with another possible reason that this occurs.

I had an older web application built in 2.0. I migrated it to a 4.5 solution.

The application built and debugged just fine when inside of Visual Studio, but then when I attempted to Publish the web application, this error occurred over and over.

I finally discovered the problem was that Build Action for the web.config file was "Embedded Resource" rather than "Content". Also, the Copy To Output Directory was set to "Always Copy" rather than "Do not copy". I do not know when these settings were made, but I believe it was back in the 2.0 version of the application.

Modifying the settings for the web.config file allowed the Publish action in Visual Studio 2012 publication to work flawlessly.

3
  • had tried every other solution out there. this one helped. My Project was migrated over time from Visual Studio 2010 to 2015, so maybe the "Always Copy" was the default value in 2010 Feb 3, 2016 at 16:34
  • The Properties for my web.config are limited to two: FileName, and FullPath; otherwise, I would check what the Build Action for it was. Dec 9, 2016 at 23:05
  • the same for me after 10 hours, bloody copy set on the web.config file
    – JamesP
    Feb 5 at 14:58
13

I had the same problem in an MVC project. The error occurred when I tried publishing. Turned out the obj folder should be empty (or at least not contain any web.config).

Running Clean didn't do the trick for me.

I solved the problem by cleaning the obj folder before any build (building of the project won't take that long anyway in my case).

I unloaded the project, and added the following to the BeforeBuild Target

<Target Name="BeforeBuild">
    <Delete Files="$(SolutionDir)\$(ProjectDir)\bin\**\*.*" />
    <Delete Files="$(SolutionDir)\$(ProjectDir)\obj\**\*.*" />
    <RemoveDir Directories="$(SolutionDir)\$(ProjectDir)\bin" />
    <RemoveDir Directories="$(SolutionDir)\$(ProjectDir)\obj" />
    <Message Text="Clean obj/bin from web project" />
</Target>

Hope this helps

7

I experienced this error only during publishing of the application.

The properties of the web.config (and transformations) files were set as:

  • Build Action - None
  • Copy to Output - Always.

The solution was to change the settings to:

  • Build Action - Content
  • Copy to Output - Do not Copy
7

"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 had this problem in VS.NET. Turned out that when I was configuring some config transforms I had mistakenly set the Web.config file's property "Copy always". I usually set my transform files to "Copy always" but leave the web.config root file as "Do not copy".

Watch out tho, because changing the properties of web.config also changes all the nested transforms.

So, to fix:

1) Change web.config to "Do not copy"

2) Optionally, if you're using config transforms, set them to "Copy always"

3) Delete the obj and bin folders from the solution (these may not be visible so select the project node in Solution Explorer and click the "Show all files" toolbar button.

4) Publish

Worked for me.

0
5

Click on the Web.config file from Solution explorer and right click "Properties" and change to "Copy to Output Directory: Do not copy".

enter image description here

0
4

I also had this issue and it came about after i used the Publishing Wizard to publish my site to the web.

After much digging around I came across this Bug report on the Connect website, https://connect.microsoft.com/VisualStudio/feedback/details/779737/error-allowdefinition-machinetoapplication-beyond-application-level

An MS rep replied and as well as explaining why this was a problem that occured when publishing he also included a temporary workaround that fixed the issue for me.

4

Delete and create the virtual Directory again. Right Click and Convert the virtual Directory to "Application"

2

Here is another reason - If you copy your entire web app into one of its own sub-folders, you will get this error. I managed to do this to an old site when copying from one machine to another - just been asked to look at the site after a gap of about 2 years and the error occurred. Took a fair bit of figuring out - as I had no multiple config files.

1
  • And how to resolve this problem ?. I have same issue when copy the folder of my wcfrest services to a new domain
    – maniaq
    May 21, 2022 at 20:17
1

For me the reason was that the obj folder was under the web site folder and multiple web.config appeared after building different configurations. I solved the problem under vs2012 by movong the obj folder out from the web site. To do this I've added manualy (in the notepad) $(SolutionDir)\Obj\$(Configuration) to every configureation in the web site project file.

1

I was having the same issue when I would publish the site, if I build the site I get no issues but while publishing I would get this awful 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"

I tried everything that has been stated here in this post to no resort, what worked for me was to just create a new publish profile with exactly the same as the one I've been using and that works well, don't get the error with the new profile but do with the old. Not sure what the difference is but at least I can publish my MVC project.

Hope this helps somebody!!

1

I had this issue, and resolved by cleaning my solution of old assemblies etc.

from vs: Build > Clean Solution

then Rebuild.

1

Windows start -> open Sites -> IIS -> right click your site -> Manage Web Site -> Advance Setting -> browse Physical Path -> try to select sub-folder of the once you are selection currently.

the logic is that the web config file inside the subfolder is trying to make changes and this is not permitted, has to be the selected folder : http://scottonwriting.net/sowblog/archive/2010/02/17/163375.aspx

0
1

I also get this error when try to deploy a sub website in website.

The solution is:

  1. You must remove some config tabs like: profile, membership, roleManager, sessionState in the sub web.config
  2. Change Authentication to None as: <authentication mode="None" />
  3. And go to IIS right click on sub folder -> Add Application.
  4. Reset IIS to resolve this issue.

IF get other issue don't hesitate ping me, maybe I will find to help.

1
  • I have not access to iis of the host .
    – maniaq
    May 21, 2022 at 20:30
1

It was fine on localhost but when I published a release on the server, I started the same error for few pages. Then I cleaned up the solution and rebuild & published, things got fixed.

1

I received this error on localhost in Visual Studio 2017, and a simple restart of Visual Studio cleared the issue.

I realise this issue can also be caused by having more than one web.config; one within a sub-folder for example. if you do have multiple web.config files purposely for another application: ensure the directory is not being seen as a virtual directory.

1

Check the path of the web.config that opens if click on error. Some node_modules, (node_module\selenium_webdriver\lib\test\data\web.config), also can have web.config file.

1

Delete obj and bin directly with fix that issue immediately.

0

Make sure you dont' fall in trap of accessing your local site wrong via localchost/mysite.test which should be mysite.test which will give you this error.

When you access your site like localhost/dir_name, in this case, your web.conf falls below root level and hence this error.

0

I was migrating apps and the app had multiple apps(multiple web.configs) within it.. what I did was go into IIS then right click on the sub folders then "Convert to Application" and it worked.

0

I got this error differently than everyone else:

I was migrating from vs2010 with web deployment project to vs2012 and a new web publish profile.

I created a new web publish project in vs2012 to publish to file system (we have a separate installer builder this is a commercial app) and I was publishing to a folder that was inside the existing web project which is linked to IIS.

This caused the error during publish which mystified me at first because I was publishing to file system, not IIS (I thought).

Solution was to change the publish to folder to outside the web project.

0

Sometimes, the simple answer is best. I had two web.config files in my project. The one at the main level is where I needed to make the change to deal with my session timeout (which triggered this issue). I had a separate config file in my Razor Views directory, which had settings for Razor and its views. I was adding a section in there (not at the application level!). Without realizing I had two separate web.config files, I tried everything except looking for the obvious.

0

my mistake was accidentally copy pasting a web.config inside another folder on the web server

0

I got this error when I forgot to convert the published project into an application within IIS.

0

In my case I found that i have another Web.config file at old folder inside web application I just delete the old web.config file and every thing is OK

0

Comparing a project that was working to the one that had the above issue I made the following change. This resolved my issue.

From:

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

To:

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.