Locking Sections of Web.Config to Prevent Overwrite from Sub folder’s Web.Configs

ASP.NET's web.config file provide easy yet powerful place to keep your configuration settings. One such power comes with <location /> section. There are situations where you may want to define some properties and lock it down so any sub folder web config file can't overwrite it, one such situation is where you have multiple applications and you want those applications to follow some rules and not allow them to overwrite that with their own settings so in order to do that you can lock sections of your web config in root web config and when all the other applications will inherit from root they will not be able to overwrite this.

In order to understand how to achieve this, we will lock clientIDMode section in web config to always have "predictable" as an option.

so in your applications root web config file create a <location> section right above <system.web> with following settings as shown below:

<location allowOverride="false">

<system.web>

<pages clientIDMode="Predictable"></pages>

</system.web>

</location>

<system.web>

<compilation debug="false" targetFramework="4.0" />

 

now, create another folder in your application and add one aspx file as well as web.config file with following settings:

<configuration>

<system.web>

<pages clientIDMode="AutoID"></pages>

</system.web>

</configuration>

 

After placing this code in sub folder's web config file if you try to browse internal folder's default.aspx file, you will get following error:

Parser Error Message: This configuration section cannot be used at this path. This happens when the site administrator has locked access to this section using <location allowOverride="false"> from an inherited configuration file.

Also, compiler will complain about this section being locked and can't be overwritten.

Tags: , ,

ASP.NET