简体   繁体   中英

how to Read Ihttpmodule from web.config system.web and system.webServer

web.config looks like:


   <system.web>
   <httpModules>
  <add name="DotNetCasClient" type="DotNetCasClient.CasAuthenticationModule,DotNetCasClient"/>
    </httpModules>
  </system.web>
     <system.webServer>
     <modules>
    <remove name="DotNetCasClient"/>
    <add name="DotNetCasClient" type="DotNetCasClient.CasAuthenticationModule,DotNetCasClient"/>
      </modules>
 </system.webServer>

In C# Code:

  [assembly: PreApplicationStartMethod(typeof(CasClientStart), "Start")]

 namespace Dev.CasClient
 {

public static class CasClientStart
{

    /// <summary>
    ///     Starts the application
    /// </summary>
    public static void Start()
    {
        if( !..... Registered (DotNetCasClient) In Web.config)
        DynamicModuleUtility.RegisterModule(typeof(DotNetCasClient));

    }
   }
   }

how to Read httpmodule from web.config ? before Dynamic Register Module,I want check Web.confg at first.


my solution,

   // the Final Solution
    public static void Start()
    {
       var IWantReg = typeof(CasClientModule).FullName;
        var Configuration = WebConfigurationManager.OpenWebConfiguration("~");
    if (HttpRuntime.UsingIntegratedPipeline)
    {
        var websermodules = Configuration.GetSection("system.webServer");

        var xml = websermodules.SectionInformation.GetRawXml();

        XDocument xmlFile = XDocument.Load(new StringReader(xml));
        IEnumerable<XElement> query = from c in xmlFile.Descendants("modules").Descendants("add") select c;

        foreach (XElement band in query)
        {
            var attr = band.Attribute("type");

            var strType = attr.Value.Split(',').First();

            if (strType.ToLower() == IWantReg.ToLower())
                return;
        }
    }
    else
    {
        object o = Configuration.GetSection("system.web/httpModules");
        HttpModulesSection section = o as HttpModulesSection;
        var models = section.Modules;

        foreach (HttpModuleAction model in models)
        {
            if (model.Type.Split(',').First() == IWantReg)
                return;
        }
    }


    DynamicModuleUtility.RegisterModule(typeof(CasClientModule));

}

终于解决了这个问题,分别通过集成与经典格式两种方式。 感谢朋友们的帮助。

Would this work for you? (untested)

Configuration Configuration =  WebConfigurationManager.OpenWebConfiguration("~");
object o = Configuration.GetSection("system.web/httpModules");
HttpModulesSection section = o as HttpModulesSection;
var kvp = section.CurrentConfiguration.AppSettings.Settings["Name"];

Try using something like

(HttpModulesSection)ConfigurationManager.GetSection("system.web/httpModules");

this will read the modules section of the web.config file. If you want to read only the loaded modules use:

HttpModuleCollection modules = HttpContext.Current.ApplicationInstance.Modules;
foreach (string moduleKey in modules.Keys)
{
    IHttpModule module = modules[moduleKey];
    // Do what you want with the loaded module
}

more information: Detecting if a HttpModule is loaded

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM