codecompleting
codecompleting

Reputation: 9611

Does the appsettings file attribute override what is in the app.config?

The appsettings tag in the app.config has a file attribute:

<appSettings file="other.config">
..
..
</appSettings>

How does this work? Will it merge what is in the appSettings (original) with the other.config file? Or will it overwrite it? What if the other.config file doesn't exist, should it crash?

I'm trying it myself and if a key isn't in the original, it doesn't seem to read it from the other.config?

Should the other.config file have just xml nodes, or should it all be inside a appsettings element?

<appSettings>
  <userId>123</userId>
</appSettings>

or

<userId>123</userId>

Upvotes: 31

Views: 19274

Answers (2)

DoctorMick
DoctorMick

Reputation: 6793

  • If the file doesn't exist it will not crash, it will just be ignored.
  • The external config has to contain the <appSettings> node so your first example is correct.
  • The value in the external file will take priority, if no value is present then the app.config value is used.

Does that cover off everything?

Upvotes: 56

JJS
JJS

Reputation: 6658

One of the best answers on the subject is here: ASP.NET web.config: configSource vs. file attributes - Credit to @Massimiliano Peluso

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

Upvotes: 7

Related Questions