C# 从辅助网络配置读取应用设置
我创建了第二个web配置并将其放置在一个文件夹中: ~/Configuration/OtherConnections.config 我的配置文件如下所示:C# 从辅助网络配置读取应用设置,c#,asp.net,web-config,C#,Asp.net,Web Config,我创建了第二个web配置并将其放置在一个文件夹中: ~/Configuration/OtherConnections.config 我的配置文件如下所示: <?xml version="1.0"?> <configuration> <appSettings> <add key="serverurl" value="http://serverUrl" /> <add key="UserName" value="myUser
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="serverurl" value="http://serverUrl" />
<add key="UserName" value="myUser" />
<add key="Password" value="XXXXXXX" />
</appSettings>
</configuration>
我得不到任何回报。这是因为web配置在一个文件夹中,还是此web应用程序中正在进行其他操作?每个web文件夹只能有一个
web.config
文件
无论如何,有两种选择:
- 在IIS管理器中,您需要将子文件夹配置为新的应用程序。它使用运行应用程序中的web.config文件
- 另一个选项是使用单个配置文件并添加一个
部分来分割文件,以便对某些文件夹或文件采取不同的操作。(我建议提供更多信息)
~/Configuration/Web.config
,但必须明确地打开它:
var config = WebConfigurationManager.OpenWebConfiguration("~/Configuration");
然后从中读出:
string url = config.AppSettings.Settings["serverurl"].Value;
请注意,您不能指定(因此不能更改)实际的
web.config
文件名。只是文件夹 您可以使用WebConfiguration Manager方法访问多个配置文件。添加命名空间:
using System.Web.Configuration;
因此,要访问的appSettings
。/SomeProjectFolder/Environment/Web.config
,您可以执行以下操作:
var config = WebConfigurationManager.OpenWebConfiguration("~/SomeProjectFolder/Environment/");
string username = config.AppSettings.Settings["username"].Value;
希望这能有所帮助。是什么让你想到可以这样做的?你的
otherConnections.config
应该位于你的Web表单的父目录中才能使用它。一个Web应用程序可以为每个文件夹设置一个Web.config。
var config = WebConfigurationManager.OpenWebConfiguration("~/SomeProjectFolder/Environment/");
string username = config.AppSettings.Settings["username"].Value;