我的网站是这样构建的:
-
根目录
-
拱廊
- default . aspx
- web . config
- default . aspx
- web . config
-
拱廊
我有一个方法Method1()
访问:
System.Configuration.ConfigurationManager.AppSettings["Total_Unique_Plays_Required_For_High_Score_Board"]
root/Arcade/Web.config
文件中有,root/web.config
文件中没有。
当我从/arcade
目录中的页面执行Method1()
时,它工作得很好。然而,当我从global.asax
执行这个方法作为一个定时事件时,它在根web.config
文件中搜索值并抛出一个System.NullException
。
有人知道我如何指定搜索root/arcade/web.config
文件中的值,而不是root/web.config
文件吗?
可以打开网页。
调用this将加载子文件;注意,您给出了包含web的文件夹的路径。配置,而不是实际的配置文件。
var config = WebConfigurationManager.OpenWebConfiguration("~/Arcade");
你现在可以得到你的值,像:
string MyValue = config.AppSettings.Settings["MySetting"].Value;
你也可以通过调用
来获取应用程序设置列表:KeyValueConfigurationCollection appSettings = config.AppSettings.Settings;