2017-05-15 71 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(); 
    } 
}