Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>
like image 819
Seb Nilsson Avatar asked Aug 04 '11 10:08

Seb Nilsson


People also ask

Is Web config file sensitive?

Web. config files may contain sensitive data. To better protect this data and strengthen the security of SharePoint, SharePoint now restricts access to its Web.

What web config file contains?

web. config file is an XML-based configuration file used in ASP. NET-based applications to manage various settings that are concerned with the configuration of our website. In this way, we can separate our application logic from configuration logic.

What is config source?

Overview. ConfigSources are templates based on Groovy or Powershell scripts that define how LogicMonitor should access, collect, and alert on your devices' configuration files.


1 Answers

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

like image 86
Massimiliano Peluso Avatar answered Oct 15 '22 06:10

Massimiliano Peluso