My Web.Config.xml file is configured a set of supported extensions to clients' http requests. These requests are handled by the same HttpHandler
implementation. I use the extensions to enable functionality in the handler. Below is a copy of the structure.
<system.webServer>
<handlers accessPolicy="Read, Execute, Script">
<add name="Handler1" path="*.path1" verb="*" type="namespace.class, assembly" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode" />
<add name="Handler2" path="*.path2" verb="*" type="namespace.class, assembly" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode" />
<add name="Handler3" path="*.path3" verb="*" type="namespace.class, assembly" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode" />
<add name="Handler4" path="*.path4" verb="*" type="namespace.class, assembly" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode" />
</handlers>
</system.webServer>
I wish to implement a 5th handler so that clients can make an initial request to get the supported paths (functionality), so that they will not try to make requests that are not supported. I wish to control enabled functionality by adding / removing handlers.
How can I get a list of the configured handlers run-time in my Handler implementation?
I wish to use the list to construct my response.
I have looked at System.Web.Configuration.HttpHandlersSection
but when I try to get system.webServer
section, I get an System.Configuration.IgnoreSection
object back.
I found that to read the system.webServer/handlers
, you need to reference the
Microsoft.Web.Administration.dll
in place of System.Configuration
.
The dll can be found in the \windows\system32\inetsrv folder when you have enabled the IIS Management Console in Windows features.
Here is some sample code:
/// <summary>
/// Returns a list of configured handler names
/// </summary>
/// <param name="filter">the handler name must contain this value to be included in the list</param>
/// <returns>a list of handler names that matches the filter or all handler names if filter is null</returns>
public static List<string> GetHandlerNames(string filter)
{
string websiteName = System.Web.Hosting.HostingEnvironment.ApplicationHost.GetSiteName();
Configuration o = srvMgr.GetWebConfiguration(websiteName);
ConfigurationElementCollection c1 = o.GetSection("system.webServer/handlers").GetCollection();
if (filter != null)
{
return c1.Where(x => x.GetAttribute("name").Value.ToString().ToLowerInvariant().Contains(filter.ToLowerInvariant())).Select(x => x.GetAttributeValue("name").ToString()).ToList();
}
else
{
return c1.Select(x => x.GetAttributeValue("name").ToString()).ToList();
}
}
/// <summary>
/// Returns a list of configured handler paths
/// </summary>
/// <param name="filter">the handler name must contain this value to be included in the list</param>
/// <returns>a list of handler paths that matches the filter or all handler paths if filter is null</returns>
public static List<string> GetHandlerPaths(string filter)
{
string websiteName = System.Web.Hosting.HostingEnvironment.ApplicationHost.GetSiteName();
Configuration o = srvMgr.GetWebConfiguration(websiteName);
ConfigurationElementCollection c1 = o.GetSection("system.webServer/handlers").GetCollection();
if (filter != null)
{
return c1.Where(x => x.GetAttribute("name").Value.ToString().ToLowerInvariant().Contains(filter.ToLowerInvariant())).Select(x => x.GetAttributeValue("path").ToString().Replace("*.", "")).ToList();
}
else
{
return c1.Select(x => x.GetAttributeValue("path").ToString()).ToList();
}
}