Thursday, September 19, 2013

FIX: 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 teach two six week courses on ASP.NET at the University of California - San Diego Extension. The first class serves as an introduction to ASP.NET and as such many of the students create their first ASP.NET websites in class. During the first meeting students create a simple ASP.NET website. At the end of the first evening, those students who did not bring their own laptop need to work on their website from home or work; they may copy the files to a thumb drive or ZIP up the files and send them via email. In either case, it's not uncommon for students to bump into the following error when opening the website to bump into the following error message: 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. This error message isn't the most sensible error message, especially those to ASP.NET, so let me try to explain it. 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 element is one such example. The above error message indicates that there is a 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. This problem most commonly arises when you open the website in Visual Studio but accidentally open the parent of the root folder. For example, imagine that you have a website located at C:\MyProjects\Website1, where the Website1 folder is the root of the website. When you open this website from Visual Studio you are asked to specify the website's root folder - it is imperative that choose the Website1 folder. If you accidentally select the MyProjects folder then the Web.config file in the Website1 folder is now in one of the website's subfolders. Because that Web.config file includes the element, among other application-level settings, you'll receive the above error. The fix to this error (in most cases), then, is to close your project and reopen it from Visual Studio, making sure that you select the appropriate folder. Happy Programming!

No comments:

MS in Computer Science with paid training in USA company