Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

managing configurations in web.config

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?

like image 249
Sam Avatar asked Dec 30 '25 00:12

Sam


2 Answers

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>
like image 192
fenix2222 Avatar answered Dec 31 '25 15:12

fenix2222


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>
like image 37
Ricardo Sanchez Avatar answered Dec 31 '25 16:12

Ricardo Sanchez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!