[英]reading web.config from class library
i have two project 1) class library with no inteface just an api 2) web application 我有两个项目1)类库,没有接口只是一个api 2)Web应用程序
from web apps i will be calling the class library api 从网络应用程序我将调用类库api
so i have all the web.config settings in the web application but when i debug it always return me null value and here is the code snippit: 所以我在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???
}
}
}
i have also tried, creating a new app.config in the class library project and have the same appsettings there but does not work... 我也试过,在类库项目中创建一个新的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>
any help? 任何帮助?
your syntax is incorrect, it should be 你的语法不正确,应该是
<configuration>
<appSettings>
<add key="employeeDB" value="Data Source=servername;Initial Catalog=employee;Persist Security Info=True;User ID=userid;Password=password;"/>
</appSettings>
</configuration>
or more correctly, since it's a connection string, 或者更准确地说,因为它是一个连接字符串,
<configuration>
<connectionStrings>
<add name="employeeDB" connectionString="Data Source=servername;Initial Catalog=employee;Persist Security Info=True;User ID=userid;Password=password;"/>
</connectionStrings>
</configuration>
which would be read by ConfigurationManager.ConnectionStrings["employeeDB"]
这将由
ConfigurationManager.ConnectionStrings["employeeDB"]
读取
Also, the System.Configuration assembly is not automatically added to a class library. 此外,System.Configuration程序集不会自动添加到类库中。
Your tag is wrong..it should be 'appSettings' not 'applicationSettings' 你的标签是错误的..应该是'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>
just saw the post and i had same problem but i got a way.. add System.Web.Configuration
reference to your class library prj then 刚看到帖子,我有同样的问题,但我有办法..添加
System.Web.Configuration
引用你的类库prj然后
ConnectingString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
Hope this will help 希望这会有所帮助
appSettings is should be like appSettings应该是这样的
<appSettings>
<add key="employeeDB" value="xxxx" />
</appSettings>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.