Web.config保存问题
本文关键字:问题 保存 config Web | 更新日期: 2023-09-27 18:01:01
我想通过web应用程序的前端向用户公开一些web.config设置。我可以毫无问题地检索设置,但保存时会出现错误,或者更改未持久保存到web.config文件中。我正在VS.中调试
如果我运行这个:
private void SaveWebConfig()
{
Configuration webConfig = WebConfigurationManager.OpenWebConfiguration("~//Web.config");
webConfig.AppSettings.Settings["DocumentPath"].Value = this.txtDocumentsDirectory.Text;
webConfig.Save();
}
我得到以下错误:无法为请求的配置对象创建配置文件。
如果我运行这个代码,什么都不会发生:
private void SaveWebConfig()
{
Configuration webConfig = WebConfigurationManager.OpenWebConfiguration("~//Web.config");
webConfig.AppSettings.Settings["DocumentPath"].Value = this.txtDocumentsDirectory.Text;
webConfig.SaveAs("~//Web.config");
}
据我所知,使用web应用程序不应更改web.config。ASP.NET和IIS的构建是为了在每次更新web.config时重新启动整个应用程序。
与其公开它公开数据库中的设置并将这些设置保存在数据库中,您的前端不应该像加载和保存数据那样做太多更改。
这是可以做到的,而且相当容易。它是否有效取决于运行应用程序的用户帐户的权限。
您使用的是双斜杠,SaveAs也是错误的。尝试:
private void SaveWebConfig()
{
Configuration webConfig = WebConfigurationManager.OpenWebConfiguration("~");
webConfig.AppSettings.Settings["DocumentPath"].Value =
this.txtDocumentsDirectory.Text;
webConfig.Save();
}
但您可能应该尽可能避免更改(根(web.config。我只在SiteManager进行配置更改的特殊页面中看到过这一点。