sd_dracula
sd_dracula

Reputation: 3896

.net and .config files

I have the following stored in a stores.config file inside my asp.net website's root folder.

<configuration>
    <appSettings>
        <add key="ClientId" value="127605460617602"/>
        <add key ="RedirectUrl" value="http://localhost:49548/Redirect.aspx"/>
    </appSettings>
</configuration>

How can I use string Clientid = ConfigurationManager.AppSettings["ClientId"].ToString(); to call it up from that file? Calling it as it of course does not work as it is looking for it in web.config.

I do not want to put the appSettings in the web.config file. Is that allowed?

Upvotes: 1

Views: 229

Answers (1)

Philip Fourie
Philip Fourie

Reputation: 116827

You can reference your stores.config file from web.config

<configuration>
  <appSettings file="stores.config">
  </appSettings>
<configuration>

Your stores.config file should have the following structure:

<appSettings>
    <add key="ClientId" value="127605460617602"/>
    <add key ="RedirectUrl" value="http://localhost:49548/Redirect.aspx"/>
</appSettings>


Alternatively you can also use: ConfigurationManager.OpenMappedExeConfiguration Method (ExeConfigurationFileMap, ConfigurationUserLevel)

For example:

// Map the new configuration file.
var configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = "stores.config";


 // Get the mapped configuration file
var config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

var clientid = config.AppSettings["ClientId"];

Upvotes: 5

Related Questions