【问题标题】:reading web.config from class library从类库中读取 web.config
【发布时间】:2016-02-17 21:12:35
【问题描述】:

我有两个项目 1)没有接口的类库只是一个api 2) 网络应用

我将通过网络应用调用类库 api

所以我在 Web 应用程序中有所有 web.config 设置,但是当我调试它时总是返回空值,这里是代码片段:

 public static string readingDB
        {
            get
            {
                string result = null;
                result = ConfigurationManager.AppSettings["employeeDB"]; //conn string
                if (!string.IsNullOrEmpty(result))
                {
                    return result;
                }
                else
                {
                    return "";  //???? THROW EXCEPTION???
                }
            }
        }

我也尝试过,在类库项目中创建一个新的 app.config 并在那里具有相同的 appsettings 但不起作用...

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <applicationSettings>
    <add name="employeeDB" connectionString="Data Source=servername;Initial Catalog=employee;Persist Security Info=True;User ID=userid;Password=password;"/>
  </applicationSettings>
  <customErrors mode="On"/>
</configuration>

有什么帮助吗?

【问题讨论】:

    标签: asp.net web-config app-config


    【解决方案1】:

    你的语法不正确,应该是

    <configuration>  
      <appSettings>  
        <add key="employeeDB" value="Data Source=servername;Initial Catalog=employee;Persist Security Info=True;User ID=userid;Password=password;"/> 
      </appSettings>  
    </configuration>  
    

    或者更准确地说,因为它是一个连接字符串,

    <configuration>  
      <connectionStrings>  
        <add name="employeeDB" connectionString="Data Source=servername;Initial Catalog=employee;Persist Security Info=True;User ID=userid;Password=password;"/>  
      </connectionStrings>  
    </configuration>  
    

    这将被ConfigurationManager.ConnectionStrings["employeeDB"]阅读

    【讨论】:

    • 感谢您的正确,但我的问题仍未得到解答,我是否应该在两个地方都有条目? (web.config 和 app.config)
    • @AbuHamzah:你应该只有 ASP.NET 网站的 Web.config。
    • +1 你是唯一一个真正指出 OP 应该使用 &lt;connectionStrings&gt; 的人。
    【解决方案2】:

    此外,System.Configuration 程序集不会自动添加到类库中。

    1. 在解决方案资源管理器中右键单击项目
    2. 点击添加>参考
    3. 检查“程序集”>“框架”选项卡中的 System.Configuration

    【讨论】:

      【解决方案3】:

      刚刚看到帖子,我遇到了同样的问题,但我有办法.. 将System.Web.Configuration 引用添加到您的类库prj 那么

      ConnectingString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

      希望这会有所帮助

      【讨论】:

      • WebConfigurationManager.ConnectionStrings["ConnectionString"] 这对我有用,谢谢@nilantha-ekanayake
      【解决方案4】:

      你的标签是错误的。它应该是“appSettings”而不是“applicationSettings”

      <?xml version="1.0" encoding="utf-8" ?>
      <configuration>
        <appSettings>
          <add name="employeeDB" connectionString="Data Source=servername;Initial Catalog=employee;Persist Security Info=True;User ID=userid;Password=password;"/>
        </appSettings>
        <customErrors mode="On"/>
      </configuration>
      

      【讨论】:

      • 感谢您的更正,但我的问题仍未得到解答,我是否应该在两个地方都有条目? (web.config 和 app.config)
      • 不,你只需要web应用的web.config中的条目。
      【解决方案5】:

      appSettings 应该是这样的

      <appSettings>
          <add key="employeeDB" value="xxxx" />
      </appSettings>
      

      【讨论】:

        猜你喜欢
        • 2013-07-06
        • 2014-09-15
        • 2010-12-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-13
        • 2015-07-03
        • 1970-01-01
        相关资源
        最近更新 更多