Hello everyone.
I have an asp.net app and was trying to simplify access to a Web Configuration object so I wrapped it into a static property. Everything is fine, but when I try to modify AppSettings key it just doesn't work!
public class Test
{
public static Configuration Config
{
get
{
return WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
}
}
public static void SaveAppSettings()
{
Config.AppSettings.Settings.Add("direct", "DIRECT");
Config.Save(ConfigurationSaveMode.Modified);
Configuration cfg = Config;
cfg.AppSettings.Settings.Add("bypass", "BYPASS");
cfg.Save(ConfigurationSaveMode.Modified);
}
}
Pre-conditions: both "direct" and "bypass" keys do exist in app settings in web.config. Execution results in "bypass" property being rewritten in web.config. And "direct" is not. In debug i can see that line
Config.AppSettings.Settings.Add("direct", "DIRECT");
does not actually modify a property, and line
cfg.AppSettings.Settings.Add("bypass", "BYPASS");
does. Why?
I have an asp.net app and was trying to simplify access to a Web Configuration object so I wrapped it into a static property. Everything is fine, but when I try to modify AppSettings key it just doesn't work!
public class Test
{
public static Configuration Config
{
get
{
return WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
}
}
public static void SaveAppSettings()
{
Config.AppSettings.Settings.Add("direct", "DIRECT");
Config.Save(ConfigurationSaveMode.Modified);
Configuration cfg = Config;
cfg.AppSettings.Settings.Add("bypass", "BYPASS");
cfg.Save(ConfigurationSaveMode.Modified);
}
}
Pre-conditions: both "direct" and "bypass" keys do exist in app settings in web.config. Execution results in "bypass" property being rewritten in web.config. And "direct" is not. In debug i can see that line
Config.AppSettings.Settings.Add("direct", "DIRECT");
does not actually modify a property, and line
cfg.AppSettings.Settings.Add("bypass", "BYPASS");
does. Why?
Last edited: