2017-05-15 4 views
2

My Web.Config.xmlファイルは、クライアントのhttp要求に対するサポートされている拡張機能のセットです。これらの要求は同じHttpHandler実装によって処理されます。拡張機能を使用して、ハンドラの機能を有効にします。以下はその構造のコピーです。設定された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> 

私は、彼らがサポートされていない要求を作成しようとしないように、クライアントは、サポートパス(機能)を取得するための最初の要求を行うことができるように、第五ハンドラを実装したいです。私は、ハンドラを追加/削除することで有効な機能を制御したいと考えています。

ハンドラの実装で設定されたハンドラの実行時のリストを取得するにはどうすればよいですか?

リストを使用して自分の応答を作成したいと考えています。

私は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を読むためにそれを発見した、あなたはSystem.Configurationの代わりに

Microsoft.Web.Administration.dll

を参照する必要があります。

dllは、Windowsの機能でIIS管理コンソールを有効にした場合、\ windows \ system32 \ inetsrvフォルダにあります。ここで

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(); 
    } 
} 
関連する問題