2013-07-18 28 views
4

web.config中的樣子:如何閱讀從web.config中的System.Web的IHttpModule和system.webServer


<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> 

在C#代碼:

[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)); 

    } 
    } 
    } 

如何讀取HTTP模塊從web.config?動態註冊模塊之前的 ,我想首先檢查Web.confg。


我的解決方案,

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

} 

終於解決了這個問題,分別通過集成與經典格式兩種方式。感謝朋友們的幫助。

回答

2

將這工作爲你? (未經測試)

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

嘗試使用像

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

這個東西會讀取web.config文件的模塊部分。如果你想爲只讀加載的模塊使用:

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

的更多信息: Detecting if a HttpModule is loaded

+0

'HttpContext.Current'空 – zbw911

相關問題