获取已配置的 HttpHandler 列表
Getting a list of configured HttpHandlers
我的 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 实现中获取已配置的处理程序列表 运行-time?
我希望使用该列表来构建我的回复。
我查看了 System.Web.Configuration.HttpHandlersSection
但是当我尝试获取 system.webServer
部分时,我得到了一个 System.Configuration.IgnoreSection
对象。
我发现要阅读 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();
}
}
我的 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 实现中获取已配置的处理程序列表 运行-time?
我希望使用该列表来构建我的回复。
我查看了 System.Web.Configuration.HttpHandlersSection
但是当我尝试获取 system.webServer
部分时,我得到了一个 System.Configuration.IgnoreSection
对象。
我发现要阅读 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();
}
}