Programming the web.config File Using C#

Add Flexibility to Your Web Applications

Joydip Kanjilal

October 30, 2009

3 Min Read
ITPro Today logo in a gray background | ITPro Today

At times we need a way to manipulate the web.config fileprogrammatically at run time. Modifying the web.config file was quitecumbersome 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 fileprogrammatically. This article discusses how this can be accomplished (withrelevant 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 configurationfile. It is typically used to configure an ASP.NET Web application and definethe configuration settings for the Web application. It typically contains theapplication-wide settings, such as database connection string, culturesettings, authentication, and authorization information, etc. In ASP.NET 1.x, mucheffort was required to manipulate the web.config file programmatically. WithASP.NET 2.0 however, this can be done quite easily and efficiently. Thefollowing 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 offlexibility in that it allows us to add or edit a configuration file seamlesslyin ASP.NET. The WebConfigurationManager class in the System.Web.Configurationnamespace has the OpenWebConfiguration method that can be used to open theconfiguration file of the application as a Configuration object for readingfrom or writing to the configuration file. The virtual path to theconfiguration file is specified to this method as a parameter. The followingcode snippet displays all the keys of the appSettings section of the web.configfile:

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 keyvalue 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 keyin 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 canbe a handy solution in some situations, it is not recommended to do sofrequently in a Web application, as any change in the web.config file will restartthe Web server and refresh the cache entries.

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

 

References

Please refer to the following links for further informationon this topic:

Working extensively in Microsoft technologies for more than 10years, Joydip Kanjilal is a SeniorProject Leader for a company in a Hyderabad,India. Hisprogramming 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].

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like