OdeToCode IC Logo

A System.Configuration Object Represents a Merged Configuration View

Monday, July 2, 2007

Look! Actual ASP.NET content is back!

The documentation for WebConfigurationManager.OpenWebConfiguration says:

"Opens the Web-application configuration file as a Configuration object using the specified virtual path to allow read or write operations."

This might lead you to think you'll only find stuff from a single web.config file in the returned Configuration object - but this isn't true. The following code will return the extensions and paths mapped to the web forms PageHandlerFactory for "MyWebSite".

IEnumerable GetPageHandlerFactoryExtensions()
string sectionName = "system.web/httpHandlers";
string pageHandlerFactoryName = typeof(PageHandlerFactory).ToString();

Configuration config =
HttpHandlersSection handlersSection = config.GetSection(sectionName)
as HttpHandlersSection;

foreach (HttpHandlerAction httpHandlerAction in handlersSection.Handlers)
if(httpHandlerAction.Type == pageHandlerFactoryName)
yield return httpHandlerAction.Path;

... and you should at least get a result of ".aspx". This ".aspx" entry probably isn't in the web.config for "MyWebSite", but it is in the machine level web.config file (found in %Windows%\Microsoft.NET\Framework\v2.0.50727\CONFIG) by default. 

Remember .NET configuration files are hierarchical, and if you look at the documentation for System.Configuration you'll find (emphasis mine):

The Configuration class instance represents the merged view of the configuration settings that apply to a specific physical entity, such as a computer, or to a logical entity, such as an application, or a Web site.