ASP.NET web.config: configSource vs. file attributes

62,594

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

Share:
62,594

Related videos on Youtube

Seb Nilsson
Author by

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, 2022

Comments

  • Seb Nilsson
    Seb Nilsson almost 2 years

    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>
    
  • Ed Graham
    Ed Graham almost 11 years
    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
    Ed Graham almost 11 years
    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 ...
  • Leniel Maccaferri
    Leniel Maccaferri over 9 years
    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.
  • Piotr Kula
    Piotr Kula over 9 years
    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?
  • frankhommers
    frankhommers over 8 years
    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..
  • Tomer Yoskovich
    Tomer Yoskovich over 8 years
    Is there a way to have that reversed? "will merge (and override) settings in the .config file"
  • Steven Liekens
    Steven Liekens over 8 years
    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?
  • tibx
    tibx about 7 years
    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.
  • Jarrod
    Jarrod over 6 years
    Updated link for appSettings element - docs.microsoft.com/en-us/dotnet/framework/configure-apps/…