0

I need a second .config to manage alot of keys. I tried using

  <configSections>
         <section name="apiConnection" type="CustomConfig.apiConnectionSection, CustomConfig" />
  </configSections>
  <apiConnection configSource ="ApiConnection.config"/>

Where "ApiConnection.config" is my .config file to manage keys but this didn't work. Then i tried the "file" property in appSettings.

<appSettings file="ApiConnection.config">   

This didn't work either. I Tried with:

../ApiConnection.config

~/ApiConnection.config

But nothing... Some ideas?

The program doesnt break, just not show me the keys when i try with the ConfigurationManager.

https://i.stack.imgur.com/6xHK2.png

<img src= https://i.stack.imgur.com/6xHK2.png/>

EDIT

My file is in root path (with web.config)

The file looks like

<?xml version="1.0" encoding="utf-8"?>
<appSettings>
  <add key="Secret" value="z8xyHkN3Eju2TS9u-4MXeI2AbZiuTsF7xYJcjIJ" />
  <add key="Audience" value="keyforms"/>
</appSettings>
  • It's hard to tell from the limited samples you have above but you may be trying to externalize at too high of a level. Take a look at this stack overflow question and see if it helps. https://stackoverflow.com/questions/1562679/moving-a-custom-configuration-group-to-a-separate-file – Brandon Johnson Nov 06 '20 at 12:33
  • Thanks for your help! I tried that way in the first example i gave. (I am going to edit it so that it is well understood). – Paul Kremser Nov 06 '20 at 12:38
  • Those edits do help a lot, Unfortunately. I'm not seeing why this wouldn't work. Is there an error message you are getting from IIS? – Brandon Johnson Nov 06 '20 at 12:53
  • nop, when i try use ConfigurationManager.AppSettings does not show me the keys. (empty list) – Paul Kremser Nov 06 '20 at 12:58

1 Answers1

0

Ok I think I know what your problem is based on your last comment.

This code is creating a new configuration section called apiConnection.

  <configSections>
         <section name="apiConnection" type="CustomConfig.apiConnectionSection, CustomConfig" />
  </configSections>
  <apiConnection configSource ="ApiConnection.config"/>

This section's values will not be contained in app settings. So you won't be able to access it via

ConfigurationManager.AppSettings

You will need to access it in a different manner. Now the exact manner will depend on your implementation of CustomConfig.apiConnectionSection and CustomConfig. I would search your code to find the class that defines how this works.

This example shows how to pull values from a custom config section, SecureAppSettings that uses the NameValueCollection in the same manner as AppSettings. You will have to do some digging to figure out what Types you will need to utilize.

NameValueCollection section = (NameValueCollection)ConfigurationManager.GetSection("secureAppSettings");
string userName = section["userName"];
Brandon Johnson
  • 534
  • 4
  • 12