如何在运行时修改 web.config appSettings? [英] How do you modify the web.config appSettings at runtime?

查看:47
本文介绍了如何在运行时修改 web.config appSettings?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对如何在运行时修改 web.config appSettings 值感到困惑.例如,我有这个 appSettings 部分:

I am confused on how to modify the web.config appSettings values at runtime. For example, I have this appSettings section:

<appSettings>
  <add key="productspagedesc" value="TODO: Edit this default message" />
  <add key="servicespagedesc" value="TODO: Edit this default message" />
  <add key="contactspagedesc" value="TODO: Edit this default message" />
  <add key="aboutpagedesc" value="TODO: Edit this default message" />
  <add key="homepagedesc" value="TODO: Edit this default message" />
 </appSettings>

比方说,我想在运行时修改homepagedesc"键.我尝试了 ConfigurationManager 和 WebConfigurationManager 静态类,但设置是只读"的.如何在运行时修改 appSettings 值?

Let's say, I want to modify the "homepagedesc" key at runtime. I tried ConfigurationManager and WebConfigurationManager static classes, but the settings are "read-only". How do I modify appSettings values at runtime?

更新:好的,所以我在这里 5 年后.我想指出,经验告诉我,我们不应该将任何在运行时有意可编辑的配置放在 web.config 文件中,而应该将它放在一个单独的 XML 文件中,正如下面一位用户所评论的那样.这不需要对 web.config 文件进行任何编辑来重新启动应用程序,这将导致愤怒的用户打电话给您.

UPDATE: Ok, so here I am 5 years later. I would like to point out that experience has told me, we should not put any configuration that intentionally is editable at runtime in the web.config file but instead we should put it in a separate XML file as what one of the users commented below. This will not require any of edit of web.config file to restart the App which will result with angry users calling you.

推荐答案

需要使用WebConfigurationManager.OpenWebConfiguration():例如:

Dim myConfiguration As Configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~")
myConfiguration.ConnectionStrings.ConnectionStrings("myDatabaseName").ConnectionString = txtConnectionString.Text
myConfiguration.AppSettings.Settings.Item("myKey").Value = txtmyKey.Text
myConfiguration.Save()

我认为您可能还需要设置 AllowLocation.这是一个布尔值,指示是否可以使用该元素配置单个页面.如果allowLocation"为false,则无法在单个元素中进行配置.

I think you might also need to set AllowLocation in machine.config. This is a boolean value that indicates whether individual pages can be configured using the element. If the "allowLocation" is false, it cannot be configured in individual elements.

最后,如果您在 IIS 中运行您的应用程序并从 Visual Studio 运行您的测试示例,这会有所不同.ASP.NET 进程标识是 IIS 帐户、ASPNET 或 NETWORK SERVICES(取决于 IIS 版本).

Finally, it makes a difference if you run your application in IIS and run your test sample from Visual Studio. The ASP.NET process identity is the IIS account, ASPNET or NETWORK SERVICES (depending on IIS version).

可能需要授予 ASPNET 或 NETWORK SERVICES Modify 访问 web.config 所在文件夹的权限.

Might need to grant ASPNET or NETWORK SERVICES Modify access on the folder where web.config resides.

这篇关于如何在运行时修改 web.config appSettings?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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