Programming the web.config File Using C#

Add Flexibility to Your Web Applications

At times we need a way to manipulate the web.config file programmatically at run time. Modifying the web.config file was quite cumbersome in ASP.NET 1.x. There was no built-in support, as such. However, ASP.NET 2.0 provides an elegant way of editing the web.config file programmatically. This article discusses how this can be accomplished (with relevant source code). See "Upgrading to ASP.NET 2.0 " for migration tips and " The ASP.NET 2.0 Portal Framework " for an overview on how to customize the appearance, layout, and behavior of your content.

 

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:

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#:

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#:

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.

I hope this article has provided the readers a head start to working programmatically with the web.config file in ASP.NET using C#. Queries and suggestions are welcome.

 

References

Please refer to the following links for further information on this topic:

Working extensively in Microsoft technologies for more than 10 years, Joydip Kanjilal is a Senior Project Leader for a company in a Hyderabad, India. His programming skills include C, C++, Java, C#, VB, VC++, ASP.NET, XML, and UML. He has worked with .NET and C# for more than five years. Reach Joydip at mailto:[email protected].

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish