C# 如何在web.config的节属性中使用appsettings值

C# 如何在web.config的节属性中使用appsettings值,c#,asp.net,asp.net-web-api,nhibernate,C#,Asp.net,Asp.net Web Api,Nhibernate,我想在web.cofig的用户定义部分使用appsettings值 <coredistributedcache factory-class="NHibernate.Caches.CoreDistributedCache.Redis.RedisFactory,NHibernate.Caches.CoreDistributedCache.Redis"> <properties> <property name="configuration">12

我想在web.cofig的用户定义部分使用appsettings值

<coredistributedcache factory-class="NHibernate.Caches.CoreDistributedCache.Redis.RedisFactory,NHibernate.Caches.CoreDistributedCache.Redis">
    <properties>
      <property name="configuration">127.0.0.1:6379</property>
    </properties>
  </coredistributedcache>
这可能吗


或者有其他方法避免重复吗?

配置不是动态的。但是,您可以使用来手动操作它

此方法可从以下方面帮助您:


这回答了你的问题吗?我认为在配置本身中没有办法。如果您正在使用第三方工具(如Octopus)执行部署,则可以使用如下参数执行配置更新,在这种情况下,两个位置将使用Octopus中定义的单个值替换相同的占位符。这可能对本地开发人员的构建没有什么帮助。抛开这比在配置中放置两次IP地址要复杂得多的事实不谈,这真的有效吗?我希望在创建应用程序域时读取Nhibernate配置,这意味着之后的更改不会影响已经生成的Nhibernate实例。你知道情况是否如此吗?你肯定是对的,同意你的看法。我的回答增加了复杂性,我们可以说它更像是一种变通方法。但这可能会有帮助。。
<add key="RedisServer" value="127.0.0.1:6379" />
<property name="configuration">{{RedisServer}}</property>
var configFile = WebConfigurationManager.OpenWebConfiguration("~");
ClientSettingsSection section = (ClientSettingsSection)configFile.SectionGroups["applicationSettings"].Sections[0];
var value = section.Settings;
SettingElement settingElement = new SettingElement();
SettingValueElement settingValueElement = new SettingValueElement();
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("~/Web.config"));
XmlNodeList nodeList;
XmlNode root = doc.DocumentElement;
nodeList = root.SelectNodes("applicationSettings");
var valuenode = nodeList[0].SelectNodes("coredistributedcache/properties/property")[0];
var oldValue = valuenode.InnerText;
valuenode.InnerText = "New Value1";
settingValueElement.ValueXml = valuenode;
settingElement.Value = settingValueElement;
value.Clear();
value.Add(settingElement);
configFile.Save();