Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read AppSettings from a secondary webconfig

I created a second web config and placed it in a folder:

~/Configuration/OtherConnections.config

My config file looks like:

<?xml version="1.0"?>

<configuration>
  <appSettings>
    <add key="serverurl" value="http://serverUrl" />
    <add key="UserName" value="myUser" />
    <add key="Password" value="XXXXXXX" />
  </appSettings>
</configuration>

When I attempt to read the value from one of the items like:

string connectionInfo = ConfigurationManager.AppSettings["UserName"];

I do not get a value back. Is this because the web config is in a folder, or is there something else going on in this web app?

like image 929
rross Avatar asked Mar 20 '12 14:03

rross


2 Answers

I do not get a value back. Is this because the web config is in a folder ... ?

No, not the folder but the filename. You can use ~/Configuration/Web.config but then you have to explicity open it:

var config = WebConfigurationManager.OpenWebConfiguration("~/Configuration");

And then to read from it:

string url  = config.AppSettings.Settings["serverurl"].Value;

Note that you cannot specify (and thus not change) the actual web.config file name. Just the folder.

like image 122
Henk Holterman Avatar answered Sep 30 '22 14:09

Henk Holterman


you can have only one web.config file for each web folder

There are tow options anyway:

  • In the IIS Manager you need to configure the sub folder as a new application. It uses the web.config file from the running app.

  • Another option is using a single config file and adding a <location> section to segment the file to act differently for some folders or files. (which I would suggest more info here)

like image 45
Massimiliano Peluso Avatar answered Sep 30 '22 14:09

Massimiliano Peluso