- Back to Home »
- WebConfigurationUtility - one of the newborns
Posted by : Michael Mortensen
Tuesday, April 23, 2013
There are still areas in the .NET Framework where I ask myself why? Why was this obvious method not part of the otherwise comprehensive collection of methods?
What I am talking about here is of course a way to get custom HTTP handlers. For reasons unknown to me, you cannot get a collection of HTTP handlers as you can with HTTP modules on the HttpApplication Class. What you can try instead is to open a web.config file using the WebConfigurationManager Class, but surprisingly this does not provide access to custom HTTP handlers from IIS7+, as this StackOverflow article also confirms.
So what can you do?
Well (for me at least) the most obvious solution was to bypass the ConfigurationManager while supporting elements from it, and simply provide ways to open a Web-application configuration file and afterwards provide ways to get specific areas of this file. I know - one could just parse the XML and find the area of interest them self, but I want to lend a helping hand in these matters so the code is kept as clean without the clutter of XPath expressions.
So, with the above in mind, I decided to create a static class named WebConfigurationUtility shown in Figure 1, where I added the methods i needed up front for my latest development additions while looking for ways to re-factor some of my older code to use these new methods; all while keeping the compatibility to IIS6 and newer.
![]() |
| Figure 1: WebConfigurationUtility members |
To provide an example, you can see in Figure 2 that we are opening a Web-application configuration file from the root of an ASP.NET application. From here we call the GetHandlers method which in return gives us an IEnumerable of the HttpHandlerAction Class which we can then iterate and work with as we are used to.
IXPathNavigable webConfig = WebConfigurationUtility.OpenWebConfiguration("/");
IEnumerable<HttpHandlerAction> handlers = WebConfigurationUtility.GetHandlers(webConfig);
foreach (Type endpointType in EndpointRoutes.Keys)
{
foreach (HttpHandlerAction handler in handlers)
{
if (endpointType == CallingAssembly.GetType(handler.Type))
{
EndpointHandlers.Add(endpointType, handler);
}
}
}
Well, that is all I have for now in regards to the WebConfigurationUtility. I hope this gave a little insight in finding ways to support and - to some extend - improve existing functionality.

