10

In my app, I have a web.config file with an appSettings section. The appSettings section contains a number of keys that the app uses. The appSettings section also contains the file="AppSettings.config" attribute. The AppSettings.config file then contains a subset of the values from the main web.config. The idea is for the web.config to contain all the base/default settings, and then to provide overrides of the defaults in the AppSettings.config file.

According to this post (https://stackoverflow.com/a/6940086/216160), my setup ought to work (particularly : will merge (and override) settings in the .config file).

The problem I'm seeing is that its not working. I have a default value of false, which then drives some logic about displaying some beta reporting functionality (or not), and have set the AppSettings.config to override this key to 'true'. Sadly, it continues to hide the report system. But, if I change the web.config value, then the item displays.

Is it possible that the AppSettings are not getting merged? How can I test/prove what's really happening?

EDIT

It appears that there was some kind of error in the AppSettings.config file. When all is working as it should, the merge happened exactly as expected. However, I still have the issue of how to detect when the AppSettings.config file has some kind of issue. I'd tested to see if the file was valid XML (and it was), but yet, some issue remained. When I copied the functioning key from web.config, and pasted it right below the non-working key from AppSettings.config, they appeared to be identical. I expect there must be some way to throw an error in the event of a config file merge error?

David Gardiner
  • 16,892
  • 20
  • 80
  • 117
reidLinden
  • 4,020
  • 4
  • 31
  • 46
  • As a random thought : in your external file, you may try to play with the element, see if it can remove the keys defined in the main file. If so, try to remove, then add the key in the external file. – jbl Sep 16 '13 at 16:10

4 Answers4

23

I just had the same issue (configs not merging as expected), but after explicitly deleting the /bin and /obj directories from the solution-folder and performing a rebuild, everything worked as expected again, so I would suggest you try that and see how it works...

PS: Also make sure you set the file properties of the external config to 'Copy Always'. Otherwise it won't exist in the bin-directory where your running application lives.

David Gardiner
  • 16,892
  • 20
  • 80
  • 117
Wim.van.Gool
  • 1,290
  • 1
  • 10
  • 19
3

I was able to confirm that the external app.config works with a simple project.

app.Config (in same directory as web.config)

<appSettings>
  <add key="testAppConfigString" value="APP string exists!"/>
  <add key="testOverrideString" value="!!OVERRIDE string exists in app.config!"/>
</appSettings>

web.config

...
  <appSettings file="app.config">
    <add key="testWebConfigString" value="web config string exists!"/>
    <add key="testOverrideString" value="OVERRIDE string exists in web.config!"/>
  </appSettings>
...

Default.aspx

...
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

    web: <asp:Label runat="server" ID="lblWeb" /><br/>
    app: <asp:Label runat="server" ID="lblApp" /><br/>
    override: <asp:Label runat="server" ID="lblOverride" /><br/>

</asp:Content>
... 

Inside the Default.aspx.cs

    protected void Page_Load(object sender, EventArgs e)
    {
        lblWeb.Text = ConfigurationManager.AppSettings["testWebConfigString"];
        lblApp.Text = ConfigurationManager.AppSettings["testAppConfigString"];
        lblOverride.Text = ConfigurationManager.AppSettings["testOverrideString"];
    }

The resulting page should have the following text:

web: web config string exists!
app: APP string exists!
override: !!OVERRIDE string exists in app.config!
caspian
  • 1,804
  • 14
  • 14
  • Thanks! In general, I know that this works exactly like you've tested it. My question is more about the failed merge aspect of things. SOMETHING was wrong, but I didn't really know that it was. Nothing told me that it wasn't, except that the behavior thats controlled by the settings was wrong. It would be nice to be able to trap whatever the error was, and notify someone that, say, maybe there's a config error here..syntax error, xml error, etc...Instead of deleting & re-adding, and magically having it start working again. I'm glad it's working, but I'd like to know why it wasn't! – reidLinden Sep 16 '13 at 18:11
  • Interesting... With the project I created from scratch I do receive an error page if there's bad text in the external config. What version of .net and visual studio are you using? – caspian Sep 16 '13 at 18:34
2

You can access multiple config files by using WebConfigurationmanager method. add namespace:

using System.Web.Configuration;

So, to access the appSettings of

../SomeProjectFolder/Environment/Web.config, you can do:

var config = WebConfigurationManager.OpenWebConfiguration("~/SomeProjectFolder/Environment/");
string username = config.AppSettings.Settings["username"].Value;

Hope this helps.

bluwater2001
  • 7,829
  • 5
  • 24
  • 21
  • I get "System.ArgumentException: 'The application relative virtual path '~/' is not allowed here.'" when I use this syntax. – fix Jul 06 '18 at 16:11
0

Perhaps worth mentioning that with Web.config those connectionStrings/@configSource and appSettings/@file are relative to project directory (not target directory). That had me for awhile.

Carl Krig
  • 161
  • 7