Web.config中保存的问题 [英] Web.config save problem

查看:161
本文介绍了Web.config中保存的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过Web应用程序的前端揭露一些web.config设置给用户。没有问题,我可以检索的设置,但是当我救我要么得到一个错误或者更改不会保存到web.config文件。我调试在VS

如果我运行此:

 私人无效SaveWebConfig()
    {
        配置webConfig = WebConfigurationManager.OpenWebConfiguration(〜// Web.config中);
        。webConfig.AppSettings.Settings [DocumentPath]值= this.txtDocumentsDirectory.Text;
        webConfig.Save();
    }

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

如果我运行这个code,什么都不会发生:

 私人无效SaveWebConfig()
 {
     配置webConfig = WebConfigurationManager.OpenWebConfiguration(〜// Web.config中);
     。webConfig.AppSettings.Settings [DocumentPath]值= this.txtDocumentsDirectory.Text;
     webConfig.SaveAs(〜// Web.config中);
 }


解决方案

这是可以做到的,也是相当容易。它是否有效取决于您的应用程序下运行的用户帐户的权限。

您使用的是双正斜杠,而另存为是错误的了。尝试:

 私人无效SaveWebConfig()
 {
     配置webConfig = WebConfigurationManager.OpenWebConfiguration(〜);
     webConfig.AppSettings.Settings [DocumentPath]。值=
          this.txtDocumentsDirectory.Text;
     webConfig.Save();
 }

但你可能应该避免尽可能改变(根)web.config中。我只在特殊页面看到本作SiteManager进行配置更改。

I want to expose some of the web.config settings to a user via the front end of the web app. I can retrieve the settings without a problem, but when I save I either get an error or the changes are not persisted to the web.config file. I am debugging in VS.

If I run this:

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

I get the following error: A configuration file cannot be created for the requested Configuration object.

If I run this code, nothing happens:

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

解决方案

It can be done, and it is rather easy. Whether it works depends on the privileges of the user account that your App runs under.

You are using double forward slashes, and the SaveAs is wrong too. Try:

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

But you probably should avoid changing the (root) web.config as much as possible. I've only seen this in special pages for the SiteManager to make config changes.

这篇关于Web.config中保存的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆