简体   繁体   中英

Change web.config programmatically

I have developed a custom module which uses httpmodules which need to be added to the web.config when my module is installed and removed from it when my module is uninstalled. What I have been doing until now is modifying my web.config file manually adding the necessary section just after the module is installed and removing it just before the module is uninstalled. The section looks like the following:

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

Now I would like to know whether it would be possible to automate this task, ie modify the web.config programmatically. I have googled but they don't work for me. Is there any method in asp.net for this problem. Thank you.

You could do something like this:

using System.Web.Configuration;

// ...

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

Also, I think, this will work as well (no need to specify the configuration source explicitly):

using System.Configuration;

// ...

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

(although, the above code requires adding a reference to System.Configuration ).

Once you have an instance of HttpModulesSection , use its Modules property to add or remove modules.

Hope this helps.

I would not suggest you to do it. You give to much power to the application users. They should not be allowed to change application configuration options. If these are user oriented configuration settings, consider placing them in database. You should also keep in mind, that the user account should have the proper permissions to access the configuration file in the file system (this makes it even a worse idea).

Anyway, perhaps this code snippet (changing an application setting) would help you to do it, if you adjust it:

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

Hope I helped!

 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");
        }

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