Dynamically Changing ASP.Net Web Config File

The Dynamic ASP.Net Web Config Update Example
The Dynamic ASP.Net Web Config Update Example

1. Dynamic Web Settings

The Web.config file supplies the Website settings required for the ASP.Net web sites. These settings are like connection strings, how many users allowed, etc. Sometimes the situation arises where we need to add or remove the configuration settings without re-starting the server. This is to avoid website’s down-time. In this article, we will see a demo of how to change web config file when the site is running. Imagine that web admin of the site will access the page which we will create in this example. The goal is to add an extra web setting dynamically, and in the meantime, the server must function as usual with no re-start.

2. About This Dynamic Web Config Change Example

The page design screenshot for this example is below.

The Dynamic ASP.Net Web Config Update Example
The Dynamic ASP.Net Web Config Update Example

In the username section, which is in the bottom of this example page, we add the session variable for the username text field. So when the user clicks the Set Username button, we store username text fields in a session variable. The Get Username button will fetch the session variable content and shows that in the label next to it.

In the Application Settings section of the page, we can add a web setting dynamically into the web.config file by clicking the Store button. The user will provide a setting name, and an associated value to it in the corresponding text boxes. By clicking the Retrieve button, we can get the setting value from the web.config file by supplying the setting name. One can use this to check they retrieve the dynamically added setting value.

In this demo, we will add an extra setting by clicking the Store button and make sure that session we set before this operation is kept unchanged. This way we can make sure that changing the web config dynamically by the web admin does not affect the existing user sessions.

3. User Session Variable

A session denotes a user’s browser keeping a connection to the website, and it goes on until he/she cuts off from it. Now the session variable is one which has the scope of the connected user session and the variable goes out-of-scope when he/she disconnects the web session. Therefore, once we define and give a value to a session variable, the content is available for all the pages he/she visits within that session. In our sample, we store and retrieve the username as the session variable.

3.1 Setting the Username Session

In the button click handler for the ‘Set User Name’, we set the content of the username in a session variable called User Name. Here, the user name is the Key, and the value is whatever we specify in the text box next to ‘Set User Name’.

3.2 Reading Session Variable

When ‘Get User Name’ button is clicked, we read the ‘User Name’ session variable and display that in the lblSession Label. Before doing so, we are checking the session variable ‘ User Name’ is having any value. If we don’t have a value, then we show a message to state session is lost. The message becomes absurd when the session is not yet created. This session variable is just for demo purpose and our goal is changing the web application settings without losing the session variables. So, we no need to worry about this validation message and how valid it is in a mixture of cases.

3.3 Testing Session Variables

In the clear button click event handler, we cleared some of the form fields for convenience for entering the new values.

OK, let us test what we had written in the two buttons’ click events. The video below shows setting and retrieving the username in a session variable called User ‘User Name’.


Video Steps

  1. The video runs the solution in Debug Mode.
  2. Then, it sets the user name for the session clicking the Set User Name button.
  3. Finally, it retrieves the username from session context by clicking the Get User Name button.

4. Retrieve Web Config Settings

4.1 The WebConfigurationManager

We use the <strong>WebConfigurationManager</strong>  to fetch the application settings, which are already saved in the web.config file. When we click the Retrieve button after filling the setting name, our example will bring back the settings value and shows it in the value text box. The code for that is below.

Have a look at the section of code taken from the web.config file, which is given below:

In this, we have only one application setting. The setting name is ‘pageTitle’, and the value is MsTechArticles.com.  Now watch the video below, which shows retrieving the setting value for the given key pageTitle.


Video Steps

  1. In the web.config current application setting section, we preview the setting.
  2. Next, we moved to DynamicSettings.aspx and click the Run icon from the toolbar.
  3. Then we provide the setting name and click on the Retrieve button.

5. Adding AppSettings Entry At Run-Time

The Web Configuration File will store the settings under various sections. In this example, we are only looking at the AppSettings section. One can apply what they learn in this article to any configuration sections say for example ConnectionStrings. We use OpenWebConfiguration method of the WebConfigurationManager  to dynamically add an extra setting to the web configuration. Below is the code:

In the above code, we get the reference to the Configuration object by calling the OpenWebConfiguration API. It will set up the object around the configuration file, which is staying in the root directory of the web application. If it is not there, then we get the settings from the Web Config File and Tracing in ASP.Net. The configuration is the merged view of all the settings. Therefore, from the config instance, we get the Application settings to add the application specific setting name and the associated value to it. In our sample application, the setting name and its value is fed by the user (Say web Admin). Here, we add a setting dynamically by calling add method. To persist this setting in the configuration file, we make a call to the save method.

It is time to check the code written for storing the web setting. The below video shows how we stored the configuration setting dynamically using our sample web page. It also opens the web config file and shows the newly added entry. Here, we lose out session variables the moment we add a setting dynamically.


Video Steps

  1. The video runs the form in debug mode.
  2. Then it adds a first appsetting (setting1, value1) to config file.
  3. Before clicking the store button, the video stores a vale in a session variable.
  4. Adds one more pair setting2, value2.
  5. It proves that session variable is lost after adding a dynamic web config entry.
  6. At last, the video opens web.config file and shows the dynamically added web settings.

6. Dynamic Update of Web Settings without losing the Sessions

6.1 Adding New Web Config File

When we did modification in the web.config dynamically all the session variable contents are lost. This we saw in the past video. To avoid this, we should move the web settings to a new config file. ‘ASP.Net’ refers this file as ‘External Configuration File’. To have this file, we need to add a new config file called ‘settings.config’ by right-clicking the project name and picking Add New Item from the displayed content menu. The picture given below shows entering the data in the Add New Item Dialog:

Adding WebConfig File to ASP.Net Project
Adding WebConfig File to ASP.Net Project

6.2 Specify New Config File in Old Web.Config

We added the external configuration file to the project after clicking the add button in the ‘Add New Item’ dialog. Now we will go to the ‘Web.Config’ file and remove or comment out the AppSettings section. The code below shows the commented-out piece of code in the web.config file.

After commenting the above config section, we should specify the external file name in the web.config file so that web.config know AppSettings is available in the new file. The ‘Web.config’ file entry for that is below:

That is all the code change required in the configuration to make the dynamic update of the web configuration file. Now we will not lose our session variables when the web admin adds a new setting to the web configuration. This time, new settings enter the Settings.config in place of the web.config. We can watch this in the video below:


Video Steps

  1. The video opens the web page in the browser.
  2. In the web page, the video enters the new settings in the Application Settings portion of the form.
  3. The video also shows you that session variables are intact when the settings are added.

6.3 ReStartOnExternalChanges  Attribute

In the above video, we added few settings to web config file dynamically. The below picture shows those settings:

Dynamically Added WebConfig Content
Dynamically Added WebConfig Content

There is one more point that we should note here. The AppSettings by default does not require any server re-start when we append the settings dynamically. For other sections, one should always make sure that ReStartOnExternalChanges is set to False. This we can ensure by checking on the ‘machine.config’ file and the screenshot given below shows the default entry for the AppSettings.

ReStartOnExternalChanges Setting in machine.config
ReStartOnExternalChanges Setting in machine.config

One can locate the machine.config file under windows folder. For example, the for machine.config file under windows 10 machine is, C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG. Say, for example, we want to add a new connection string in the web config dynamically. The first thing we should do is check the ‘machine.config’ file and then set the restartonExternalChanges=false for a specific section.

Source Code (Download From Google Drive): Dynamic Config Example

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: