ASP.NET web.config: configSource vs. file attributes
file
attribute
- Specifies a relative path to an external file that contains custom application configuration settings
- specific to the
appSettings
section - will merge (and override) settings in the .config file
- will not cause web application to restart when modifying the specified file
- http://msdn.microsoft.com/en-US/library/ms228154(v=vs.100).aspx
- Using the Configuration.AppSettings.Settings.Add API will result in all settings being merged back into the main
.config
on a Configuration.Save call. - since .NET 1.1
- Exception is not thrown if file does not exist.
configSource
attribute
- can apply to most sections of a configuration file, not just
appSettings
- will override the entire section with the external file, no merging
- CAN cause web application to restart
- http://msdn.microsoft.com/en-US/library/system.configuration.sectioninformation.configsource(v=vs.100).aspx
- Using the Configuration.AppSettings.Settings.Add API will result in all settings being added to the file specified in
configSource
on a Configuration.Save call. - since .NET 2.0
-
System.Configuration.ConfigurationErrorsException
is thrown if config source file does not exist.
The file
attribute specifies an external file containing custom settings like you do in the appSettings entry of the web.config file.
Meanwhile, the external file specified in the configSource
attribute contains the settings for the section which you declare the configSource for. For example, if you use the configSource
attribute of the pages section, then the external file will contain the settings for the pages section.
The custom settings declared in the external config specified in the
file
attribute will be merged with the settings in theappSettings
section in theweb.config
file. In the meanwhile, theconfigSource
does not support merging, it means that you'll have to move the entire section settings into the external file.
http://www.codeproject.com/Messages/1463547/Re-difference-between-configSource-and-file-attrib.aspx
Related videos on Youtube
Seb Nilsson
Software Developer with focus on ASP.NET & C# Passionate about the web, HTML5 & JavaScript Active within Microsoft-technologies and Open Source Sharing knowledge from own projects
Updated on July 08, 2022Comments
-
Seb Nilsson almost 2 years
Within an
web.config
-file in an ASP.NET-application some sections of config, likeappSettings
andconnectionStrings
, supports the attributesfile
andconfigSource
.What is the difference between using the
file
-attribute and theconfigSource
-attribute? When should you use which attribute and can you use both?<?xml version="1.0"?> <configuration> <appSettings file="AppSettings.config"> </appSettings> <connectionStrings configSource="ConnectionStrings.config"> </connectionStrings> <!-- ... --> </configuration>
-
Ed Graham almost 11 yearsAlso, the "file" attribute allows you to specify files outside the immediate directory tree, which is important for sharing common settings among different sites. Unfortunately, the "configsource" attribute restricts you to files within the currrent tree, so for shared settings you need to specify a virtual directory in IIS.
-
Ed Graham almost 11 yearsMy above comment was not entirely correct, and I've apparently missed the somewhat arbitrary five-minute window to edit it! You can't set a virtual directory in IIS to allow "configsource" files to live outside the immediate directory tree. So that really is quite limiting. I solved it by using a junction point (or NTFS hard link) but it's not exactly pretty ...
-
Leniel Maccaferri over 9 yearsWAO! Great answer... this really helped me:
will not cause web application to restart when modifying the specified file
. I do need the app to restart when modifying any setting in the external file for a custom section and so the way to go is withconfigSource
. Interesting is that I was usingfile
and the different sections were working.file
also works for sections other thanappSettings
but there are those gotchas well explained in the answer. -
Piotr Kula over 9 yearsWhat if I want a default "connectionstring" for example but use confisection for a local file 100 developers have that is not checked in. With configsections we are forced again to check in the file completely making this exercise redundant?
-
frankhommers over 8 yearsI would like to add to the configSource list:
It must refer to a file in the same directory or in a subdirectory as the configuration file.
. And also to the file list:It can reside outside the directory of the configuration file itself.
. -
Tomer Yoskovich over 8 yearsIs there a way to have that reversed? "will merge (and override) settings in the .config file"
-
Steven Liekens over 8 yearsIf calls to
Save()
writes values to the main .config file, but those values are overridden by the (old) values in the external file, then how is this design not horribly broken? Am I missing something? -
tibx about 7 yearsAlso the file defined in configSection attribute must always exist. It can't be optional. If this file doesn't exist, IIS returns error 500.52 Specified config Source cannot be parsed.
-
Jarrod over 6 yearsUpdated link for appSettings element - docs.microsoft.com/en-us/dotnet/framework/configure-apps/…