Web.config保存问题

时间:2011-07-05 06:48:05

标签: c# asp.net web-config

我想通过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();
    }

我收到以下错误: 无法为请求的Configuration对象创建配置文件。

如果我运行此代码,则不会发生任何事情:

 private void SaveWebConfig()
 {
     Configuration webConfig = WebConfigurationManager.OpenWebConfiguration("~//Web.config");
     webConfig.AppSettings.Settings["DocumentPath"].Value = this.txtDocumentsDirectory.Text;
     webConfig.SaveAs("~//Web.config");
 }

2 个答案:

答案 0 :(得分:4)

据我所知,消费的Web应用程序不应更改web.config。构建ASP.NET和IIS是为了在每次更新web.config时重新启动整个应用程序。

不是暴露它暴露数据库中的设置并在数据库中保留这些设置,而是前端不应该只更改加载和保存数据的方式。

答案 1 :(得分:0)

可以做到,而且很容易。它是否有效取决于您的应用程序运行的用户帐户的权限。

您正在使用双正斜杠,而SaveAs也是错误的。尝试:

 private void SaveWebConfig()
 {
     Configuration webConfig = WebConfigurationManager.OpenWebConfiguration("~");
     webConfig.AppSettings.Settings["DocumentPath"].Value =
          this.txtDocumentsDirectory.Text;
     webConfig.Save();
 }

但是你可能应该尽可能避免更改(root)web.config。我在SiteManager的特殊页面中只看到了这一点,以便进行配置更改。

相关问题