Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using configurationmanager to read from multiple web.config files

Background:

I have some data thats stored in the web.config files of about 100 web applications. This data is getting moved to a database gradually. The webpages will show the web.config data until somebody clicks on an "edit" link in which case they'll be redirected to a webpage which will allow them to update this data where it will be saved in a database instead.

Problem:

Not all of the data will be changed on this page that will save it to the database. When somebody clicks the "edit" link I want the form to populate with the data from the web.config file and when they click "save" have it save to the database. However, using the configurationmanager I can only get it to pull data from the web.config file on current application.

Questions:

  1. Is there a way to use configurationmanager to select the web.config file from lets say ../{dynamic_app_id}/web.config ?
  2. is reading them as plain xml files my only option?
  3. Are there any pitfalls to this approach?
  4. Is there another solution that would work better perhaps?
like image 877
Steve's a D Avatar asked Aug 21 '12 19:08

Steve's a D


People also ask

Can we use multiple Web config files in a single application?

Yes you can have two web. config files in application. There are situations where your application is divided in to modules and for every module you need separate configuration. For example if you have a application which has two modules lets say accounts and sales.

What is the use of multiple web config file in asp net?

The web. config files are used to set settings for a certain folder and its subfolders in a cascading way, ultimately overriding the default settings that can be found in the machine.

What is ConfigurationManager configuration?

The ConfigurationManager class enables you to access machine, application, and user configuration information. This class replaces the ConfigurationSettings class, which is deprecated. For web applications, use the WebConfigurationManager class.


1 Answers

You can read any config file with ease. Please see my sample code where I read application settings from external app.config file:

        System.Configuration.KeyValueConfigurationCollection settings;
        System.Configuration.Configuration config;

        System.Configuration.ExeConfigurationFileMap configFile = new System.Configuration.ExeConfigurationFileMap();
        configFile.ExeConfigFilename = "my_file.config";
        config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(configFile, System.Configuration.ConfigurationUserLevel.None);
        settings = config.AppSettings.Settings;

Happy coding and best regards!

like image 79
Gregor Primar Avatar answered Nov 12 '22 13:11

Gregor Primar