我的 Web.Config.xml 文件配置了一组受支持的客户端扩展程序' http请求。这些请求由相同的HttpHandler
实现处理。我使用扩展来启用处理程序中的功能。以下是结构的副本。
<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>
我希望实现第5个处理程序,以便客户端可以发出初始请求以获取支持的路径(功能),这样他们就不会尝试发出不受支持的请求。我希望通过添加/删除处理程序来控制启用的功能。
如何在Handler实现中获取已配置处理程序运行时的列表?
我希望使用该列表来构建我的回复。
我查看了System.Web.Configuration.HttpHandlersSection
,但当我尝试获取system.webServer
部分时,我收到了System.Configuration.IgnoreSection
个对象。
答案 0 :(得分:0)
我发现要阅读system.webServer/handlers
,您需要参考
Microsoft.Web.Administration.dll
取代System.Configuration
。
在Windows功能中启用IIS管理控制台后,可以在\ windows \ system32 \ inetsrv文件夹中找到该DLL。
以下是一些示例代码:
/// <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();
}
}