Programming the web.config File Using C#


What Is web.config?

The web.config file is the application’s configuration file. It is typically used to configure an ASP.NET Web application and define the configuration settings for the Web application. It typically contains the application-wide settings, such as database connection string, culture settings, authentication, and authorization information, etc. In ASP.NET 1.x, much effort was required to manipulate the web.config file programmatically. With ASP.NET 2.0 however, this can be done quite easily and efficiently. The following section discusses how this can be achieved.

The Configuration API in ASP.NET 2.0

The configuration API of ASP.NET 2.0 adds a great level of flexibility in that it allows us to add or edit a configuration file seamlessly in ASP.NET. The WebConfigurationManager class in the System.Web.Configuration namespace has the OpenWebConfiguration method that can be used to open the configuration file of the application as a Configuration object for reading from or writing to the configuration file. The virtual path to the configuration file is specified to this method as a parameter.

The following code snippet displays all the keys of the appSettings section of the web.config file:

Listing – 1

        Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");

        AppSettingsSection appSettingsSection = (AppSettingsSection)configuration.GetSection("appSettings");
        if (appSettingsSection != null)
        {
            foreach (string key in appSettingsSection.Settings.AllKeys)
            {
                Response.Write(key);
            }
        }


The following method can be used to modify a specific key — value pair of the web.config file — programmatically using C#:

Listing – 2

        public void Modify(string key, string value)
        {
            Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");
            AppSettingsSection appSettingsSection = (AppSettingsSection)configuration.GetSection("appSettings");
            if (appSettingsSection != null)
            {
                appSettingsSection.Settings[key].Value = value;
                config.Save();
            }
        }

The following method can be used to delete a specific key in the web.config file programmatically using C#:

Listing – 3

        public void Remove(string key)
        {
            Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");
            AppSettingsSection appSettingsSection = (AppSettingsSection)configuration.GetSection("appSettings");
            if (appSettingsSection != null)
            {
                appSettingsSection.Settings.Remove(key);
                config.Save();
            }
        }

Conclusion

Even if modifying a web.config file programmatically can be a handy solution in some situations, it is not recommended to do so frequently in a Web application, as any change in the web.config file will restart the Web server and refresh the cache entries.

Advertisements

19 thoughts on “Programming the web.config File Using C#

  1. Hey Ramani,
    Great posting.Keep ur good work.
    I want to get a favor from you.
    I have developed a web application.I have hosted my application in two different location.In this two locations I have separate server and 50-100 users using my application through LAN.My application and DB are same in name but having different data.My requirement
    is : I want to access the two DB server located in two different locations through the same application hosted in internet.Say ADB is the DB server name located in place A,BDB is the DB server name located in place B & APP1 is the application in internet.What I am planning to do is : I will develop a page for changing the DB locations in the application hosted in internet.Can I change the Web config file for changing the DB server name.Is it a good solution.Can you please suggest some solutions

    Thanks in advance
    Binto Thomas

      1. Thanks You Ramani
        From login page only I could decide which DB I have to access.But DB connection is establishing using a common class and I am calling the instance of that class from every data access class for getting DB connection.So how can I pass the DB server details to connection class.Can I use session or…?
        Otherwise I should pass the DB server info from each and every method.
        How can we use session in class…?
        Please give me a solution.

  2. Hei Ramani

    I am using in web.config.
    How can I change each and every key and value in appSettings

    Please help me

    Regards,
    Binto Thomas

  3. Simply want to say your article is stunning. The knowledge in your post is simply impressive and i can take for granted you are an expert on this subject. Well with your permission allow me to grab your rss feed to keep up to date with future post. Thanks a million and please keep up the fabulous work.

  4. Hello,

    I want to modify the “Role” section under web config file for different 5 web pages( 5 different web configs)for the non English OS (Spanish OS administrator user present as Administrador….for other non eng OS BUILT IN names)

    below are my queries
    1. What is best way to modify config file. ( through installation or in Page Load of web page/or any other part)
    2. i am having the code to get the user roles in specific language using the SID.
    3. Where should i use above code to modify all the web config either in single go or separably

    pawarsau

  5. public void Modify(string key, string value)
    {
    Configuration configuration = WebConfigurationManager.OpenWebConfiguration(“~”);
    AppSettingsSection appSettingsSection = (AppSettingsSection)configuration.GetSection(“appSettings”);
    if (appSettingsSection != null)
    {
    appSettingsSection.Settings[key].Value = value;
    config.Save();
    }
    }

    i write bellow code for change web.config
    but error occurs in “config.Save();” not exist in current context what is config.save???

    1. your answer is very helpful for me…..
      thank u so much……..
      i change
      ” configuration.Save();” to config.Save();

      and i solve problem…..

    1. Hi , Have you tried listing 3 , Delete app settings. you can modify that listing following line for the section you are looking to delete.
      (AppSettingsSection)configuration.GetSection(“appSettings”);

      And than try to delete it. Hope this will help !!!

    1. When First time Web site is requested by user from IIS server where it is deployed, it will load the settings of web site from web.config file. Hope this will help !!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s