2017-05-15 33 views
2

Web.Config.xml文件配置为一组支持的扩展客户的HTTP请求。这些请求是由同一HttpHandler执行处理。我使用扩展来启用处理程序中的功能。以下是该结构的副本。获取配置HttpHandlers的列表

<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处理器,使客户能够做出初始请求得到支持的路径(功能),使他们不会尝试让不支持的请求。我希望通过添加/删除处理程序来控制启用的功能。

我怎样才能获取配置的处理程序列表运行时会在我的处理程序实现?

我希望用列表来构建我的反应。

我已经看过System.Web.Configuration.HttpHandlersSection但是当我尝试获取system.webServer部分时,我得到一个System.Configuration.IgnoreSection对象。

+0

看看这个https://msdn.microsoft.com/en-us/library/ms151434(v=vs.110)的.aspx –

+0

@ S.Petrosov看来,system.webServer元素不被“WebConfigurationManager.GetSection”的支持。它返回一个System.Configuration.IgnoreSection对象。 –

回答

0

我发现阅读system.webServer/handlers,你需要引用

Microsoft.Web.Administration.dll

代替System.Configuration

当您在Windows功能中启用IIS管理控制台时,可以在\ windows \ system32 \ inetsrv文件夹中找到该dll。

enter image description here

下面是一些示例代码:

/// <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(); 
    } 
}