【发布时间】:2011-06-18 00:54:51
【问题描述】:
我的应用程序中有一个自定义的 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 自我拯救?
【问题讨论】:
标签: asp.net web-config medium-trust