【问题标题】:Change web.config programmatically以编程方式更改 web.config
【发布时间】:2014-01-08 16:11:47
【问题描述】:

我开发了一个使用 httpmodules 的自定义模块,在安装我的模块时需要将其添加到 web.config 中,并在卸载我的模块时从其中删除。到目前为止,我一直在做的是修改我的 web.config 文件,在模块安装后手动添加必要的部分,并在模块卸载之前将其删除。该部分如下所示:

 <httpModules>
    <add type="QueryStringModule" name="QueryStringModule"/>
</httpModules> 

现在我想知道是否可以自动执行此任务,即以编程方式修改 web.config。我已经用谷歌搜索了,但它们对我不起作用。 asp.net 中有什么方法可以解决这个问题。谢谢。

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    你可以这样做:

    using System.Web.Configuration;
    
    // ...
    
    var config = WebConfigurationManager.OpenWebConfiguration("/web.config");
    var modules = config.GetSection("system.web/httpModules") as HttpModulesSection;
    

    另外,我认为这也可以(无需明确指定配置源):

    using System.Configuration;
    
    // ...
    
    var modules = ConfigurationManager.GetSection("system.web/httpModules") as 
      HttpModulesSection;
    

    (虽然,上面的代码需要添加对System.Configuration的引用)。

    一旦有了HttpModulesSection 的实例,就可以使用它的Modules 属性来添加或删除模块。

    希望这会有所帮助。

    【讨论】:

    • 当我使用 WebConfigurationManager.OpenWebconfiguration("/web.config") 或 ("~") 时出现无效异常错误。它显示无法映射路径“/web.config”。错误信息。我也尝试过使用xml,但它也不起作用。有什么适合我的解决方案吗?
    • @haripds 试试WebConfigurationManager.OpenWebConfiguration(null) (MSDN)
    【解决方案2】:

    我不建议你这样做。您赋予应用程序用户很大的权力。不应允许他们更改应用程序配置选项。如果这些是面向用户的配置设置,请考虑将它们放在数据库中。您还应该记住,用户帐户应该具有访问文件系统中配置文件的适当权限(这使得它更加糟糕)。

    无论如何,如果你调整它,也许这段代码 sn-p(更改应用程序设置)会帮助你做到这一点:

    Configuration myConfiguration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
    myConfiguration.AppSettings.Settings.Item("myKey").Value = ...
    myConfiguration.Save();
    

    希望我能帮上忙!

    【讨论】:

      【解决方案3】:
       public class CustomSection : ConfigurationSection
       {
         public CustomSecuritySection Security { get; private set; }
                      [ConfigurationProperty("type", IsRequired = true, DefaultValue = "QueryStringModule")]
            public String type
             {
               get { return (String)base["type"]; }
               set { base["type"] = value; }
              }
      
             [ConfigurationProperty("name", IsRequired = true, DefaultValue = "QueryStringModule")]
        public String name
         {
           get { return (String)base["name"]; }
           set { base["name"] = value; }
          }
      
         public CustomSection()
         {
      
         }
         }                
      

              Configuration config = ConfigurationManager.OpenExeConfiguration(@"D:\xxxxx\xxxx\web.config");
              //var httpmod = config.Sections.Add("TestSecton", 
      
             if (config.Sections["NewSection"] == null)
             {
              customSection = new CustomSection();
              config.Sections.Add("NewSection", customSection );
              config.Save(ConfigurationSaveMode.Full);
              //ConfigurationManager.RefreshSection("NewSection");
              }
      

      【讨论】:

        猜你喜欢
        • 2016-08-12
        • 2011-01-16
        • 1970-01-01
        • 1970-01-01
        • 2010-09-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多