Home  >  Article  >  Backend Development  >  Specific methods to modify web.config nodes in asp.net code

Specific methods to modify web.config nodes in asp.net code

高洛峰
高洛峰Original
2017-01-13 13:35:551408browse

But this variable will not have a fixed value and will change according to the actual situation. For example, when you need to read the path of a configuration file, and this path is the actual hard disk path published by the site, if it is directly the compile-time state, no problem. But if the site iis changes the path, you need to modify the parameters in this web.config. If this compile-time state can be modified into a run-time state, it will be more reasonable and convenient. This requires a solution that can dynamically modify web.config in code.
Code

 /// <summary>
          /// 写入web.config
          /// </summary>
          /// <param name="item">appSettings等</param>
          /// <param name="key">键</param>
          /// <param name="value">值</param>
          public void WriteConfig(string item, string key, string value)
          {
              if (item == "")
             {
                 item = "appSettings";
             }
             Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(System.Web.HttpContext.Current.Request.ApplicationPath);
             AppSettingsSection appSection = (AppSettingsSection)config.GetSection(item);
             if (appSection.Settings[key] == null)
             {
                 appSection.Settings.Add(key, value);
                 config.Save();
             }
             else
             {
                 appSection.Settings.Remove(key);
                 appSection.Settings.Add(key, value);
                 config.Save();
            }
         }

For more specific methods of modifying web.config nodes in asp.net code, please pay attention to the PHP Chinese website for related articles!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn