How can I simplify my Web.config file?
One of the strengths of ASP.NET is its configurable extensibility. We can hook up new features—many of which we’ve demonstrated in this book—simply by dropping binaries in the bin folder and wiring them up in Web.config.
But that’s a bit of a problem. With each new feature or option we use (ASP.NET, Ajax, providers, ASP.NET membership, ASP.NET monitoring, log4net, urlrewriting.net, SubSonic, and so on), the Web.config file keeps growing until it’s an unruly mess of XML.
How can we gain control over the Web.config file without giving up all the great features in ASP.NET?
We can use the
configSource attribute to reference external configuration
files from our Web.config file. This approach
allows us to place any configuration section into its own separate file,
which can turn our enormous, cluttered Web.config
into a lightweight file that references simple configuration files, each
of which meets a single purpose.
In ASP.NET 1.1, a Web.config file’s
appSettings element can reference an external
file via the
<appSettings file=”DevSettings.config”> <add key=”Common” value=”External file settings are merged with Web.config values” /> </appSettings>
This approach suffered a big limitation, though—the
file attribute was only available for the
appSettings element. Given that
ASP.NET 2.0’s new features have caused our
Web.config files to balloon in size, we can be glad
that these features also include the new
attribute, which is available for all elements in a
For example, here’s a simplified Web.config file that uses external configuration files to handle specific configuration elements:
<?xml version="1.0"?> <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"> <appSettings configSource="config/appSettings.config"/> <connectionStrings configSource="config/connections.config"/> <log4net configSource="config/log4net.config"/> <urlrewritingnet configSource="config/log4net.config"/> <system.web> <compilation debug="true" /> <authentication mode="Windows"/> <identity impersonate="true"/> <pages configSource="config/pages.config"/> <profile configSource="config/profile.config"/> <httpHandlers configSource="config/httpHandlers.config"/> <httpModules configSource="config/httpModules.config"/> </system.web> </configuration>
The way file references are merged hints at an interesting aspect of the way the ASP.NET configuration has been developed. The entire system is based on inheritance:
The root is the machine.config file, in the C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG directory.
The default settings for all ASP.NET sites on the server are stored in a Web.config file that resides in the same folder as machine.config. This Web.config file overrides the settings in machine.config.
The Web.config folder in the root of each web site directory inherits from the machine.config file and the default Web.config file, and overrides those settings as necessary. If you wanted to, you could give your site’s Web.config file a blank root element, in which case it would simply inherit all the default settings.
You can further override these settings within your web site’s subfolders—an approach that can be useful if your site has an Admin subfolder that requires Admin-specific settings. But, while this technique can be helpful, keep in mind that it can make site maintenance and troubleshooting slightly more complex, because settings in the Web.config file are in effect for all but that specific subfolder.
You can continue to override Web.config settings by adding Web.config folders to subfolders further down through the levels of your web site’s hierarchy.