-->

Error to use a section registered as allowDefiniti

2019-01-02 18:01发布

问题:

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.com/web.config AND domain.com/portal/web.config ... so my domain.com/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?

Any guidance would be greatly appreciated!

回答1:

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) Hope this helps.



回答2:

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.



回答3:

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.



回答4:

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.



回答5:

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.



回答6:

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.



回答7:

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



回答8:

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


回答9:

"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.



回答10:

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.



回答11:

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



回答12:

Click on the Web.config file from Solution explorer and change

Copy to Output Directory = Do not copy



回答13:

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.



回答14:

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!!



回答15:

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.



回答16:

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

from vs: Build > Clean Solution

then Rebuild.



回答17:

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



回答18:

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.



回答19:

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.



回答20:

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.



回答21:

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.



回答22:

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.



回答23:

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.



标签: