C# 使用System.Configuration.ConfigurationSection保存对web.config的更改
我已经创建了一个自定义web配置节,可以在运行时成功地读取和修改该节。但是,它不会实际更改web.config。如果重新启动站点或回收池,我将丢失更改,并将其恢复为原始web.config设置 我希望能够持久化对web.config文件的更改,但我还没有弄明白这一点 我的web.config部分如下所示:C# 使用System.Configuration.ConfigurationSection保存对web.config的更改,c#,.net,web-config,webconfigurationmanager,C#,.net,Web Config,Webconfigurationmanager,我已经创建了一个自定义web配置节,可以在运行时成功地读取和修改该节。但是,它不会实际更改web.config。如果重新启动站点或回收池,我将丢失更改,并将其恢复为原始web.config设置 我希望能够持久化对web.config文件的更改,但我还没有弄明白这一点 我的web.config部分如下所示: <configSections> <section name="MyCustomConfig" type="MyProject.Con
<configSections>
<section
name="MyCustomConfig"
type="MyProject.Configuration.myCustomConfig"
allowLocation="true"
allowDefinition="Everywhere"
/>
</configSections>
<MyCustomConfig Address="127.0.0.1"
OrgId="myorg"
User="john"/>
在我的控制器中,我使用发布的表单修改设置
[HttpPost]
public ActionResult Edit(ConfigurationViewModel config)
{
if (ModelState.IsValid)
{
// write changes to web.config
Configuration.MyCustomConfig.Current.Address = config.SIPAddress;
Configuration.MyCustomConfig.Current.User = config.User;
Configuration.MyCustomConfig.Current.OrgId = config.OrgId;
Configuration.MyCustomConfig.Save()
}
}
在config class save方法中,IsModified()返回true,现在我只需要对文件进行修改。为什么要在运行时更改配置文件?这将导致每次更改文件时应用程序池都被回收 将这些设置存储在数据库中并在应用程序启动时从数据库中恢复它们是否有意义
我只是从最佳实践的角度提出问题,因为我知道这是可能的,只是不推荐。我不知道应用程序池会在更改时回收。在数据库中存储可能会更好。
[HttpPost]
public ActionResult Edit(ConfigurationViewModel config)
{
if (ModelState.IsValid)
{
// write changes to web.config
Configuration.MyCustomConfig.Current.Address = config.SIPAddress;
Configuration.MyCustomConfig.Current.User = config.User;
Configuration.MyCustomConfig.Current.OrgId = config.OrgId;
Configuration.MyCustomConfig.Save()
}
}