【发布时间】:2015-07-03 14:40:14
【问题描述】:
我在我的 Web API 项目中启动了代码,该代码尝试从默认情况下发布在 bin 目录中的 web.config 文件中读取值。因为应用初始化点是Global.asax,所以这个级别没有web.config,所以我需要弄清楚如何在bin文件夹中引用它。
这是我的 IIS 目录的根目录:
每当我尝试使用ConfigurationManager.AppSettings 从 web.config 文件中读取值时,它们都会返回 null。只有当我将 web.config 移出 bin 文件夹并进入根目录时,才能找到值。
我尝试添加一些代码,暗示它会在尝试读取值之前强制路径到正确的默认位置(在 bin 文件夹内),但这也不起作用:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Fix web.config location
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin\\web.config");
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", path);
var appId = ConfigurationManager.AppSettings["ApplicationId"];
}
}
有没有办法强制使用 bin 文件夹中的 web.config 路径?
【问题讨论】:
标签: asp.net-web-api web-config global-asax configurationmanager