DEV Community

Ankit Vijay
Ankit Vijay

Posted on • Originally published at ankitvijay.net on

Override appSettings during development

It is a fairly common scenario that during development the local machine configuration settings for different developers in are not same and it may not match with the default value in source control.

One such example can be SQL server connection string. Some developers may have SQL Express installed, others may have a named instance on their local. However, the web.config or app.config can only have one value for this setting.

The way people (at least β€œI” used to) handle this usually is to change the configvalues on the local machine during development but discard those changes at the time of commit. If accidentally, however you forget to change the setting back to original, then there is a risk of breaking the build or code on other developer’s local machine.

Override your appSettings

Recently, my colleague (thanks James!) showed me a nice little trick handle this. I must say, I was bit embarrassed to not know this already.

You can override the key-value pairs defined under appSettings element of config by using thefile attribute.

.gist table { margin-bottom: 0; }

The file localAppSettings.config is not part of your source control. If your local settings match exactly as web.config,then you do not need to have this file on your local. Else, you can use this file to override _ only _ the settings that are different than web.config.

.gist table { margin-bottom: 0; }

When you use the application settings in your code, you get following values:

| Key –> Value |
| dbConnectionString --> localMachineConnectionString |
| someRandomApplicationSetting --> sourceControlApplicationSetting |

Important Note: If you use connectionStrings element to store your data source connection string, then, you can use configSource attribute instead. The two attributes however, are not equivalent. You can read more about the difference here.

Hope this nice little trick helps ease your development. πŸ™‚

The post Override appSettings during development appeared first on Hi, I'm Ankit.

Top comments (0)