Question

I have a series of MVC projects (20+) in the same solution. Some of them need to share the same values for a large number of app-settings.

I would like to maintain these in one common location (say "appSettings.config") in a parent directory to the relevant projects for ease of maintenance, bringing the file into web.config via the file attribute on the appSettings tag.

To bring the file into each project, I can use Add > Existing Item... and this all works, but not if I select Add As Link when I add the existing item. Without selecting add-as-link, the file is merely copied, and the values between the 2 files are not kept in sync, defeating my purpose.

Is there a way to get this to work with a linked appSettings.config file?

Was it helpful?

Solution

As you say, in web.config, you can specify that the contents of the appSettings section come from a separate file. Use this to import the shared settings. Like this:

<appSettings file="..\..\config\appSettings.config" />

The file path you specify is relative to the location of web.config. As shown above, you can simply use ..\ notation to reference a path outside of the web root. That means you can maintain the shared config file wherever is convenient.

See http://msdn.microsoft.com/en-us/library/aa903313(v=vs.71).aspx for further details.

OTHER TIPS

In order to centralize the app settings in one file and have many projects use those key value pairs, simply add a new file named appSettings.config to the root of the solution/repository, then update all the web.config files to reference that new location relative to the web.config project root directory. Examples, for one project the web config might be updated to:

<appSettings file="../appSettings.config" />

Whereas, a second project may need to have the webconfig updated to:

<appSettings file="../../../appSettings.config" />

It all depends on the location of the file relative to the location of the project. In the scenario above, 2 projects are in one solution. You can very well have as X number of projects using a single appSettings.config file.

See the link below from Microsoft. https://msdn.microsoft.com/en-us/library/aa903313(v=vs.71).aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top