我将调用类库api
所以我在Web应用程序中拥有所有web.config设置,但是当我调试它时总是返回null值,这里是代码snippit:
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>
任何帮助?
答案 0 :(得分:10)
你的语法不正确,应该是
<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"]
答案 1 :(得分:1)
你的标签是错误的..应该是'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>
答案 2 :(得分:1)
此外,System.Configuration程序集不会自动添加到类库中。
答案 3 :(得分:0)
appSettings应该像
<appSettings>
<add key="employeeDB" value="xxxx" />
</appSettings>
答案 4 :(得分:0)
刚刚看到帖子,我有同样的问题,但我有办法..
将System.Web.Configuration
引用添加到您的类库prj中
那么
ConnectingString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
希望这会有所帮助