我的应用程序中有一个自定义 ConfigurationSection:
public class SettingsSection : ConfigurationSection
{
[ConfigurationProperty("Setting")]
public MyElement Setting
{
get
{
return (MyElement)this["Setting"];
}
set { this["Setting"] = value; }
}
}
public class MyElement : ConfigurationElement
{
public override bool IsReadOnly()
{
return false;
}
[ConfigurationProperty("Server")]
public string Server
{
get { return (string)this["Server"]; }
set { this["Server"] = value; }
}
}
在我的 web.config
<configSections>
<sectionGroup name="mySettingsGroup">
<section name="Setting"
type="MyWebApp.SettingsSection"
requirePermission="false"
restartOnExternalChanges="true"
allowDefinition="Everywhere" />
</sectionGroup>
</configSections>
<mySettingsGroup>
<Setting>
<MyElement Server="serverName" />
</Setting>
</mySettingsGroup>
阅读该部分工作正常。我遇到的问题是,当我通过以下方式阅读该部分时
var settings = (SettingsSection)WebConfigurationManager.GetSection("mySettingsGroup/Setting");
然后我继续修改Server
属性:
settings.Server = "something";
这不会修改 web.config 文件中的“服务器”属性。
注意:这需要在中等信任下工作,所以我不能使用WebConfigurationManager.OpenWebConfiguration
哪个工作正常。有没有明确的方法来告诉它ConfigSection
自己保存?