1

I'm working on a ASP.NET project and I need to add some settings in appSettings section of my web-app.

Now these settings are growing up, so I'd like to organize them in different files. I've created other web.config files in different directories of my application, adding something like this:

<?xml version="1.0"?>
<configuration>
  <system.web>
  </system.web>
  <appSettings>
    <add key="settingKey" value="settingValue" />
  </appSettings>
</configuration>

But when I try to access them via ConfigurationManager.AppSettings["settingKey"], I get null.

So, is it possible to split settings in different files? Is there another way to logically organize app settings values?

davioooh
  • 23,742
  • 39
  • 159
  • 250
  • Check this out: http://stackoverflow.com/questions/11363121/connectionstring-management-for-many-projects-on-one-server-should-i-make-my-ow/11363291#11363291 – Andre Calil May 14 '13 at 16:35

2 Answers2

4

I know this is too old and probably doesn't even apply to .NET Core but for those coming from Google and using non-.NET Core json config files. Here's what I normally do...

I use configSources to take all config settings out of the web.config. This allows you to a specific config section to a different file by providing a relative location for example here's how you'd declare a configSource in a configuration section (in the root web.config file)...

<configuration>

  <log4net configSource="Config\debug\log4net.config" />
  <appSettings configSource="config\debug\settings.config" />
  <connectionStrings configSource="config\debug\connections.config" />    
  ...    
</configuration>

You can name those files whatever you want, just make sure that the path specified and files exist in the solution. Here's what the settings.config file looks like...

<?xml version="1.0"?>
<appSettings>
  <add key="webpages:Version" value="3.0.0.0" />
  <add key="webpages:Enabled" value="false" />
  <add key="ClientValidationEnabled" value="true" />
  <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  <add key="foo" value="bar" />
</appSettings>

Now, the relative path is relative to the project root...

enter image description here

In picture above you can see that I have provided two different paths for different deployment environments, that's because obviously my conection strings and settings are different in production.

Then you can use configuration transformations so that the application can use the correct config files whether it is in debug or release mode...

enter image description here

This is what the Web.Debug.config file looks like...

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <log4net configSource="Config\debug\log4net.config" xdt:Transform="Replace" />
  <appSettings configSource="config\debug\settings.config" xdt:Transform="Replace" />
  <connectionStrings configSource="config\debug\connections.config" xdt:Transform="Replace" />
</configuration>

The release one is pretty much the same...replace the paths provided to the configSource attributes.And that's pretty much it. There are other web.config elements that support configSource settings such as many of the system.serviceModel children element.

Leo
  • 14,625
  • 2
  • 37
  • 55
0

You will only be able to see the web.config settings within a directory if the currently executing path is within that directory.

So for example: /MyDirectory/web.config

is only visible if you are loading a page like: /Mydirectory/MyTestPage.aspx

You would not see the web.config settings here for example: /OtherDirectory/MyTestPage.aspx

This post may help: Creating a custom .config file in asp.net

Community
  • 1
  • 1
miskiw
  • 94
  • 6