I have an asp.net web app. I want to separate out a particular section alone, say for instance, appsettings and have it in a separate config file. How do I manage 2 config files for asp.net app? As per the example, the appsettings alone comes during deployment. Either I can add this section into web.config and have 1 config file or have the appsettings section as a separate file. Any clean way to do it?
Yes, you can extract sections out of web.config. For example, for connection strings, lets say you have
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="MyEntities" connectionString="my connection string" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
You could separate Connection string section into separate file. So your web.config becomes
<?xml version="1.0"?>
<configuration>
<connectionStrings configSource="ConnectionString.Config" />
</configuration>
And ConnectionString.Config file will have
<connectionStrings>
<add name="MyEntities" connectionString="my connection string" providerName="System.Data.EntityClient" />
</connectionStrings>
In your web.config file replace your appsettings section with the following:
<appSettings file="settings.config">
</appSettings>
and create a new config file "settings.config" that contains all of your app settings, see example below:
<appSettings>
<add key="Setting1" value="This is Setting 1" />
<add key="Setting2" value="This is Setting 2" />
<add key="ConnectionString" value="This is a ConnectString" />
</appSettings>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With