【问题标题】:Visual studion 2013 MVC5 (Getting error in reading app.config value of portable class library)Visual Studio 2013 MVC 5(读取可移植类库的 app.config 值时出错)
【发布时间】:2015-03-03 18:55:53
【问题描述】:

Visual studion 2013 MVC5(读取可移植类库的 app.config 值时出错)。我有两个项目

  1. 网络应用程序
  2. 可移植类库。

我在类库项目的 app.config 中使用了我想要的键值 读取我的类文件,但因为 System.Configuration 类在库项目中不可用,所以我无法从 app.config 中读取值:

<appSettings>
   <add key="xyz" value="123" />
</appSettings>

当我试图在类库的一个类文件中读取值时

public class mytest
{
  public static string AppValue=
      System.Configuration.ConfigurationManager.AppSettings["xyz"].ToString();
}

当我尝试使用 System.Configuration 时,我收到了 The 'configuration' element is not declared 的错误,我没有在类库中得到这个。

【问题讨论】:

  • 您可能缺少 System.Configuration 的程序集引用

标签: c# asp.net-mvc-5


【解决方案1】:

请确保您的 webconfig 文件中提供以下配置

<appSettings>
   <add key="xyz" value="123" />
</appSettings>

【讨论】:

  • 我的解决方案中有 2 个项目,一个是包含 web.config 文件的 Web 应用程序和另一个包含 app.config 文件的可移植类库项目,我想从 app.config 文件中读取键值在我的类库类文件中。我必须在我的类库项目中的 app.config 文件中添加 ,我也确实在我的 Web 应用程序中添加了这个类库项目的引用。
  • @user4327298 - app.config 类库文件被忽略。唯一自动读入配置类的配置文件是应用程序的主 EXE 文件或 Web 项目的 web.config 文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-27
  • 2012-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多