user1166905
user1166905

Reputation: 2622

ASP.Net - use seperate configuration file for database and app settings

I have an asp.net project that is using Entity Framework that is used on several different client servers. This means a different web.config for each for connectionstrings and app settings. Hasn't been an issue but I changed something that altered the web.config file recently and I manually had to adjust this for each client, I also have to exclude the web.config file in updates to ensure their own one is not overwritten.

What I would like to achieve is store these settings in maybe another config file that the project can pick up and use. Maybe that on Globals Application_Start gets these and imports them/overwrites the current web.config file perhaps.

Essentially I don't want to affect the current code that uses the connection string and ConfigurationManager.AppSettings used throughout the project but I do want to be able to let the web.config file update for each client and use a seperate file for some settings.

NOTE: I do not have access to publish directly to each server so I can't simply write a different deploy web.config for each one. I have to publish the files locally, store as zip and automated routine on servers downloads and extracts accordingly.

EDIT: Please do say if this is considered a bad idea but an idea I had was to put something similar in Global.asax Application_Start method:

  1. Does config file exist?
  2. If no, create file and append all current settings in web.config
  3. If yes, open and import those settings to the current web.config overwriting the original values

Hopefully then in a few weeks time, after I have asked all clients to perform a manual update they will have this code and I can begin to include the web.config in updates.

Upvotes: 1

Views: 218

Answers (1)

Del
Del

Reputation: 416

In VS, inside the Build menu, the last item is Configuration Manager.

In here you can specify various different release environments which can each have their own web.config transforms.

This is normally used for production/staging/test environments. However, I can see no reason why you could not use this and have a configuration file for each of your servers/environments.

You will then need to create the transformations for each environment, by rightclicking the web.config, then selecting Add Config Transform.

each of the environments you had setup can then override the settings in the main web.config. (That now acts as a template/default settings)

e.g. In Web.EnvironmentA.Config

<add key="ConnectionString" value="ConStringA" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>

In Web.EnvironmentB.Config

<add key="ConnectionString" value="ConStringB" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>

Then when you do a publish, you simply setup which config file to use. If you use Azure or the VS publish method, it will save each of these settings, so then you can simply push to the relevant environment easily.

Just make sure you test this works as you intent first, before you start publishing everywhere ;)

Upvotes: 1

Related Questions