Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [60]

By Root 5265 0

...

The element tells IIS not to validate the schema of the web.config file against the known configuration schema of integrated mode. In this way, when you are working in integrated mode, and are ignored; and when you are in classic mode, the entire section is ignored.

Note

If you’re having trouble while hosting an ASP.NET application under IIS 7.x in integrated mode, you might want to read the following article for more information and a very good background of the whole topic: http://learn.iis.net/page.aspx/381/aspnet-20-breaking-changes-on-iis-70.

Managing Configuration Data


Configuration data can be managed by developers and administrators in two main ways: programmatically through an ad hoc API, and manually through command-line utilities, XML editors, or perhaps the Web Site Administration Tool (WSAT). Let’s take a closer look at these options.

Using the Configuration API


ASP.NET includes a full configuration management API that enables you to navigate, read, and write an application’s configuration files. Configuration settings are exposed as a set of strongly typed objects that you can easily program against. These classes—one for each section in the overall schema—are all defined in the System.Configuration namespace.

The configuration API is smart enough to provide a merged view of all the settings that apply to that level. When settings are modified, the API automatically writes changes to the correct node in the correct configuration file. The management API can be used to read and write configuration settings of local and remote applications. Custom configuration sections are automatically manageable through the API.

Retrieving Web Configuration Settings


You use the WebConfigurationManager class to get access to the ASP.NET configuration files. The class is the preferred way to work with configuration files related to Web applications. The following code snippet illustrates how to retrieve the HTTP handlers in use in the current application:

void Button1_Click(object sender, EventArgs e)

{

var name = @"system.web/httpHandlers";

var cfg = WebConfigurationManager.OpenWebConfiguration("/");

var handlers = (HttpHandlersSection) cfg.GetSection(name);

EnumerateHandlers(handlers);

}

void EnumerateHandlers(HttpHandlersSection section)

{

foreach (var handler in section.Handlers)

{

...

}

}

You open the configuration file using the OpenWebConfiguration method. The parameter you pass to the method indicates the level at which you want to retrieve information. If you specify null or /, you intend to capture configuration data at the site’s root level. If you want information at the machine level, you resort to the OpenMachineConfiguration method.

The OpenWebConfiguration method returns a Configuration object on which you can call GetSection to retrieve the contents of a particular section. For HTTP handlers, you do as follows:

HttpHandlersSection section;

section = (HttpHandlersSection) cfg.GetSection(@"system.web/httpHandlers");

Each section class has a programming interface that closely reflects the attributes and child sections on the element.

To access configuration data at the application level, you pass the application’s URL to the OpenWebConfiguration method:

var path = Request.CurrentExecutionFilePath;

Configuration cfg = WebConfigurationManager.OpenWebConfiguration(path);

To retrieve information about other sections, you use the same pattern illustrated earlier by changing section names and section classes.

Note

The .NET Framework offers two similar classes to achieve the same goals: the aforementioned WebConfigurationManager and ConfigurationManager. Their functionalities overlap to a good extent, but they are not the same thing. In particular, they do the same thing if all you need to do is read data from mapped sections such as AppSettings and ConnectionStrings If you need to access a specific section, remember

Return Main Page Previous Page Next Page

®Online Book Reader