----------------------------------------------------------- New Message on MumbaiUserGroup
----------------------------------------------------------- From: Swapnil_B1 Message 1 in Discussion Configuration API - ASP.NET 2.0 The configuration API in .NET 2.0 gives us the ability to read and update configuration files, including web.config and machine.config files. You can read and write configuration files for your application, for another application on the same machine, or even an application on a different server. In this article, we will take a look at some of the highlights of the configuration API from the perspective of an ASP.NET developer, including how to use encryption and alternate configuration files. Note: but be careful while updating and modifying configuration file since it leads to application restart. AppSettings and Connection Strings Two common tasks in ASP.NET development are reading application setting strings and connection strings from the configuration file. In .NET 2.0 these settings reside in the <appSettings> and <connectionStrings> respectively. A sample web.config file for an ASP.NET site might look like the following. <?xml version="1.0"?> <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"> <appSettings> <add key="message" value="Hello World!" /> </appSettings> <connectionStrings> <add name="pubs" connectionString="..."/> <add name="northwind" connectionString="..."/> </connectionStrings> <system.web> <compilation debug="true" /> <authentication mode="Windows"/> <identity impersonate="true"/> </system.web> </configuration> The configuration API for ASP.NET developers begins with the WebConfigurationManager class in the System.Web.Configuration namespace. The WebConfigurationManager includes static (shared) properties to fetch application settings and connection string. For example, to read the message appSetting from the web.config we could use the following code: string message; message = WebConfigurationManager.AppSettings["message"]; Similarly, if we want to grab the second connection string, the connection with the name of northwing, we could use the following code: string connectionString = WebConfigurationManager.ConnectionStrings["northwind"].ConnectionString; The configuration API makes easy work of reading any setting in a configuration file using the GetSection static method. GetSection takes an XPath expression to indicate the section you want to get, and you can coerce the resulting object reference into a strongly typed reference for built-in section types. For instance, there is an AuthorizationSection class to manipulate the settings inside the <authorization> section, and a PagesSection class to manipulate the settings in the <pages> section. If we want to write out the value of the impersonate attribute in the <identity> section of web.config, we could use the following: protected void readImpersonationButton_Click(object sender, EventArgs e) { IdentitySection section; section = WebConfigurationManager.GetSection("system.web/identity") as IdentitySection; if (section != null) { WriteMessage("Impersonate = " + section.Impersonate); } } Modify Configuration Files The WebConfigurationManager class also allows us to open a web configuration for update using the static method OpenWebConfiguration. We can open a configuration file inside of our application by passing just a relative path. We can also read configuration files in other applications by passing the IIS sitename and a virtual directory. Its even possible to open application configuration files on another machine. If we want to toggle the debug attribute in the <compilation>section of the web.config for the current application from true to false and back again, we could use the following code in the event handler for a button click event: protected void toggleDebugButton_Click(object sender, EventArgs e) { Configuration config; config = WebConfigurationManager.OpenWebConfiguration("~"); CompilationSection compilation; compilation = config.GetSection("system.web/compilation") as CompilationSection; if (compilation != null) { compilation.Debug = !compilation.Debug; config.Save(); WriteMessage( "Debug setting is now: " + compilation.Debug); } } Using a strongly typed CompilationSection object allows to use to read and write the attributes inside a <compilation> section. We can make changes to this section (and any others) and save all the changes at once using the Save method of the System.Configuration.Configuration object returned from OpenWebConfiguration. Swapnil (Swaps) http://swapsnet.spaces.live.com/ ----------------------------------------------------------- To stop getting this e-mail, or change how often it arrives, go to your E-mail Settings. http://groups.msn.com/MumbaiUserGroup/_emailsettings.msnw Need help? If you've forgotten your password, please go to Passport Member Services. http://groups.msn.com/_passportredir.msnw?ppmprop=help For other questions or feedback, go to our Contact Us page. http://groups.msn.com/contact If you do not want to receive future e-mail from this MSN group, or if you received this message by mistake, please click the "Remove" link below. On the pre-addressed e-mail message that opens, simply click "Send". Your e-mail address will be deleted from this group's mailing list. mailto:[EMAIL PROTECTED]
