AppSettings on a different .config file not being updated

寵の児 提交于 2019-12-23 10:06:56

问题


I am trying to do the exact same thing mentioned on this recent previous question. In essence, here's the case (which is exactly my same situation):

My plan is to have these (appSettings) in their own file (Settings.config), to which I will grant modify permissions to the web process user account, and store all editable settings in this file (e.g. forum title, description, etc).

The problem is that the solution accepted in that question doesn't work for me because instead of saving the appSettings in the separate .config file, when I issue the config.Save(ConfigurationSaveMode.Minimal, false) command, it replicates all the appSettings of the separate file into the appSettings section of the main web.config file (with the new changes). Here's my final code (in vb.net):

Public Shared Function GetAppSetting(ByVal setting As String) As String
    Dim config As System.Configuration.Configuration = _
            WebConfigurationManager.OpenWebConfiguration("~")

    Return config.AppSettings.Settings(setting).Value
End Function

Public Shared Sub SetAppSetting(ByVal setting As String, ByVal value As String)
    Dim config As System.Configuration.Configuration = _
            WebConfigurationManager.OpenWebConfiguration("~")

    config.AppSettings.Settings(setting).Value = value

    config.Save(ConfigurationSaveMode.Minimal, False)
    ConfigurationManager.RefreshSection("appSettings")
End Sub

Basically I can't see where I would be indicating that I want the settings to be saved on the separate file instead of on the web.config which is where they are stored by default. Oh and by the way, I had to add the 'file=' attribute on the appSettings section of the web.config so that the Settings.config appSettings would be actually taken into account. Without that attribute the above code doesn't read the separate .config file settings. Here's a snapshot of my web.config appSettings section:

  <appSettings file="Settings.config">
    <add key="RestartApp" value="-1" />
  </appSettings>

And here's the entire contents of my Settings.config file:

  <appSettings>
    <add key="AppTitle" value="MVC Web Access" />
    <add key="DefaultWebpage" />
    <add key="CustomCSS" />
    <add key="TktsEmailTo" value="email@email.com" />
    <add key="EmailFrom" value="email@email.com" />
    <add key="EmailFromSMTP" value="mail.email.com" />
    <add key="EmailFromPW" value="fakePassword" />
  </appSettings>

So instead of ending up with modified settings on my Settings.config file after the .save command, my appSettings section on the web.config file ends up like this (and the Settings.config file remains untouched):

  <appSettings file="Settings.config">
    <add key="RestartApp" value="-1" />
    <add key="AppTitle" value="New title" />
    <add key="DefaultWebpage" value="index.aspx" />
    <add key="CustomCSS" />
    <add key="TktsEmailTo" value="newemail@email.com" />
    <add key="EmailFrom" value="newemail@email.com" />
    <add key="EmailFromSMTP" value="mail.email.com" />
    <add key="EmailFromPW" value="NewFakePassword" />
  </appSettings>

回答1:


Just double-checked - the only difference I can see is that I'm using

<appSettings configSource="Settings.config"/>

Here is the code I'm using now, which is working and saving settings to my separate settings file (Settings.config):

var config = WebConfigurationManager.OpenWebConfiguration("~");

foreach (var key in collection.Keys)
{
    if (config.AppSettings.Settings[key.ToString()] != null)
    {
        config.AppSettings.Settings[key.ToString()].Value = collection[key.ToString()];
    }
}

config.Save(ConfigurationSaveMode.Minimal, false);
ConfigurationManager.RefreshSection("appSettings");

What happens if you use configSource on your appSettings key?



来源:https://stackoverflow.com/questions/1799413/appsettings-on-a-different-config-file-not-being-updated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!