Home About Us SQL Interview Book Contact Us RSS
Articles
Tools
Code Snippets
Links
Tips & Tricks
FAQ
Resources
Articles
Code Snippets
Links
FAQ
Resources

Saravana Kumar
Vinod Kumar


Configuration Management in ASP.NET 2.0 - Part 2

One new, important feature in ASP.NET 2.0 is the ability to read/edit the configuration files in local machine or remote machine. In ASP.NET 1.x, editing of the configuration files was not supported. Only option to edit a configuration file is to treat the configuration file as an xml file and update that xml file. The major drawback in this method is that you need to treat all the sections in the configuration file as XML Node, which is not strongly typed. Hence, you can do only string manipulation to update any configuration settings in that file. However, in ASP.NET 2.0 you have strongly typed API’s to read/edit the configuration files.   

This article series describes the various options available in ASP.NET 2.0 to read/edit the configuration files. The various options available to read/edit the configuration files are

  1. Using Web Site Administration Tool
  2. Using ASP.NET Microsoft Management Console(MMC)
  3. Using Configuration API’sIn the first part of this article, I have explained about

In the first part of this article, I have explained first option "Using Web Site Administration Tool". In this article, we will see other two options for reading/editing configuration files.

Using ASP.NET Microsoft Management Console (MMC)

In ASP.NET 2.0, you can manage configuration file from IIS itself i.e. the configuration management API is integrated with IIS itself. This feature is available as Microsoft Management Console (MMC), which can be accessed from the properties settings of any website in. Using this console, administrator can modify both machine.config file and web.config file.

A new ASP.NET tab is included in the IIS site properties window, which is shown below.

Note: Edit machine.config button will be there only when you select properties section of a website. This wont be there when you select properties section for a virtual directory in IIS

In this tab, you can specify the version of asp.net for that web application. In previous release, if you want to change the aspnet.version for a web application, you need to use command line utility aspnet_regiis, which comes with .NET Framework. Now you can do this from IIS itself. If you want to edit machine.config, you can press Edit machine.config file button. Similarly, if you want to edit web.config file, you can press edit configuration. Both this button will invoke a separate window, which has option for editing various sections in config file. This window is show below

The configuration settings shown in this window are merged view of settings from all the levels of hierarchy i.e. from machine.config to web.config in the root folder of that application.

Using configuration API’s

Another method to edit configuration file in ASP.NET is using Configuration API’s. .NET Framework 2.0 provides strongly typed API’s for editing and reading configuration files. Both WebSiteAdministration tool and IIS MMC snapin use this API’s internally to update configuration files. Configuration class is present in System.Configuration namespace can be used to access the configuration of any application (webapplication, windows application or console application) in .NET 2.0. The configuration class has following methods,

  1. GetWebConfiguration

This method is used to the access the configuration of a web application (web.config). This method retrieves the combined configuration for a web application.

  1. GetExeConfiguration

This method is used to access the configuration of any standalone application (app.config)

  1. GetMachineConfiguration.

This method is used to access the configuration details for specific computer (machine.config)

In this article, we will concentrate on GetWebconfiguration method only. Other methods also behave in the same way. GetWebconfiguration method accepts relative URL for a web application and returns the configuration data for that website. For example,

Dim cnfg As System.Configuration.Configuration

cnfg = Configuration.GetWebConfiguration(Request.ApplicationPath)

Dim str As String

Dim conn As String

str = cnfg.AppSettings.Settings.Item("TempDir")

conn= cnfg.ConnectionStrings.ConnectionStrings.Item(1).ConnectionString

 

GetWebConfiguration method accepts relative URL (applicationpath) and returns configuration. Most of the sections in config file are available as properties in configuration class. In the above example, appsettings configuration values and connectionstring values are retrieved using AppSettings property and ConnectionString property in Configuration class. Similarly, location, section group section in config file can also be directly accessed from Configuration class. Other sections like Trace, Session can be accessed using getSection method. getSection is the generic method available in configuration class to access any settings in config by providing the path of those settings.

Following example shows how to enable trace using Configuration class,   

Dim cnfg As System.Configuration.Configuration

cnfg = System.Configuration.Configuration.GetWebConfiguration(Request.ApplicationPath)

Dim trace As System.Web.Configuration.TraceSection

trace = DirectCast(cnfg.GetSection("system.web/trace"),

System.Web.Configuration.TraceSection)

trace.Enabled = True

cnfg.Update()

Trace section is accessed using getSection method by passing path of that section (“system.web/trace”).  This method returns TraceSection class, which represents the trace section in web.config. Using this class, you can access all the properties of trace section in config file. Enabled property of trace section is accessed in the above example. If you want to update any value in configuration file after accessing that value, then you need to call update method of configuration class at the end of your changes to write the changes back to config file. Using Configuration class, you can also add custom configuration section in your configuration file using sectionGroup property.

To get the complete list of properties and method of Configuration class, check out this link.

http://msdn2.microsoft.com/library/dx9wh62s.aspx

Conclusion

One of the much requested feature in.Net Framework 1.1 is to update the .config file programmatically which has been included in .NET 2.0 as Configuration class. Other then Configuration class, now you have an option to edit the configuration file using MMC snap in and a web admin page. Even you can customize this web admin page for your application. With all these options, configuration is made simpler in .Net Framework 2.0.