从读Global.asax中Web.config文件 [英] Reading the web.config file from Global.asax

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

问题描述

我在试图读取从默认情况下的bin目录中公布的web.config文件值我的Web API项目启动code。由于应用程序初始化点是Global.asax中,有在这个级别没有web.config文件,所以我需要弄清楚如何引用它倒在bin文件夹中。

I have start up code in my Web API project that attempts to read values from the web.config file which is published inside the bin directory by default. Because the application initialization point is Global.asax, there is no web.config at this level so I need to figure out how to reference it down in the bin folder.

下面是我的IIS目录的根目录:

Here is the root of my IIS directory:

每当我尝试使用 ConfigurationManager.AppSettings web.config文件中读取的值他们回来空。当我移动的web.config起来了bin文件夹,进入根目录中找到的值,这只是

Anytime I try to read values from the web.config file using ConfigurationManager.AppSettings they come back null. It's only when I move the web.config up out of the bin folder and into the root directory will the values be found.

我尝试添加一些code,暗示它会试图读取值之前强制路径正确的默认位置(bin文件夹中),但不工作之一:

I tried adding some code that suggested it would force the path to the correct default location (inside the bin folder) before trying to read a value but that doesn't work either:

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?

Is there a way to force the path to the web.config living inside the bin folder?

推荐答案

首先,我没有得到你为什么要保持你的web.config文件中的bin文件夹。 web.config文件的默认路径总是同一文件夹中的Global.asax即应用程序的根目录。我在ASP.NET,ASP.NET的WebAPI和ASP.NET MVC应用程序一直在和他们都保持在web.config中同一文件夹中的Global.asax。

Firstly, I am not getting why are you keeping your web.config file in bin folder. Default path of web.config file is always the same folder as global.asax i.e. the root directory of the application. I have been working in ASP.NET, ASP.NET WebAPI and ASP.NET MVC applications and all of them keep web.config in same folder as Global.asax.

这有什么,如果你摆脱bin目录你的web.config文件一样Global.asax中,你将失去?

Is there anything that you will lose if you move your web.config file from bin directory to the same as global.asax?

此外,如果它是你想读的(因为我从你的<一个得到连接字符串href=\"http://stackoverflow.com/questions/29854374/no-connection-string-could-be-found-in-the-application-config-file-but-it-does\">$p$pvious问题),你可以做以下,并把你的connectionSettings在这个web.config文件bin目录。

Additionally, if it is the connection string that you want to read (as I get from your previous question) you can do below and put your connectionSettings in this web.config of bin directory.

<connectionStrings configSource="bin\connection.config"/>

有关配置文件可以在的这个文章。

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

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