210

Within an web.config-file in an ASP.NET-application some sections of config, like appSettings and connectionStrings, supports the attributes file and configSource.

What is the difference between using the file-attribute and the configSource-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>
Seb Nilsson
  • 26,200
  • 30
  • 103
  • 130

1 Answers1

337

file attribute

configSource attribute

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 the appSettings section in the web.config file. In the meanwhile, the configSource 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

DdW
  • 890
  • 1
  • 18
  • 30
Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
  • 11
    Also, 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 May 28 '13 at 16:25
  • 8
    My 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 ... – Ed Graham May 28 '13 at 16:44
  • 1
    WAO! 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 with `configSource`. Interesting is that I was using `file` and the different sections were working. `file` also works for sections other than `appSettings` but there are those gotchas well explained in the answer. – Leniel Maccaferri Nov 06 '14 at 10:10
  • What 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? – Piotr Kula Nov 28 '14 at 10:30
  • 5
    I 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.`. – frankhommers Sep 01 '15 at 15:11
  • Is there a way to have that reversed? "will merge (and override) settings in the .config file" – Tomer Yoskovich Dec 05 '15 at 09:06
  • 3
    If 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? – Steven Liekens Feb 09 '16 at 09:57
  • Also 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_. – tibx Apr 04 '17 at 15:22
  • 2
    Updated link for appSettings element - https://learn.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/appsettings/appsettings-element-for-configuration – Jarrod Oct 17 '17 at 23:09