WebConfigurationManager.Save()导致外部配置文件中的值写入Web.Config

时间:2015-02-05 14:32:08

标签: c# asp.net appsettings web.config-transform

我有一个ASP.NET应用程序,需要在web.config中更新AppSetting值(如下所示)。哪个工作正常...

方法

    public static bool UpdateAppSetting(string name, string value)
    {
        try
        {
            var config = WebConfigurationManager.OpenWebConfiguration("~/");
            config.AppSettings.Settings[name].Value = value;
            config.Save(ConfigurationSaveMode.Modified, false);
            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }

的Web.Config

  <appSettings file="custom.config">
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    <add key="UPDATETHIS" value="1.6" />
  </appSettings>

但是......正如您所看到的,我有一个custom.config文件,其中包含其他自定义AppSettings。

当我将“ UPDATETHIS ”的值更新为“ 1.7 ”时,它会更新,但它会在custom.config中进行所有设置,并将这些设置写入/保存到web.config以及新更新的值!?

似乎我不能只更新该单个值并拥有自定义appsettings配置?其他人都知道怎么解决这个问题?

0 个答案:

没有答案