从类库中读取 web.config [英] reading web.config from class library
问题描述
我有两个项目1) 没有接口的类库只有一个 api2) 网络应用程序
从网络应用程序我将调用类库 api
所以我在 web 应用程序中有所有的 web.config 设置,但是当我调试时它总是返回 null 值,这里是代码片段:
public static string readingDB{得到{字符串结果 = 空;结果 = ConfigurationManager.AppSettings["employeeDB"];//连接字符串if (!string.IsNullOrEmpty(result)){返回结果;}别的{返回 "";//????抛出异常???}}}
我也尝试过,在类库项目中创建一个新的 app.config 并在那里具有相同的 appsettings 但不起作用...
<add name="employeeDB" connectionString="Data Source=servername;Initial Catalog=employee;Persist Security Info=True;User ID=userid;Password=password;"/></applicationSettings><customErrors mode="On"/></配置>
有什么帮助吗?
你的语法不正确,应该是
<预><代码><配置><应用设置><add key="employeeDB" value="Data Source=servername;Initial Catalog=employee;Persist Security Info=True;User ID=userid;Password=password;"/></appSettings></配置>或更准确地说,因为它是一个连接字符串,
<预><代码><配置><连接字符串><add name="employeeDB" connectionString="Data Source=servername;Initial Catalog=employee;Persist Security Info=True;User ID=userid;Password=password;"/></connectionStrings></配置>由 ConfigurationManager.ConnectionStrings["employeeDB"]
i have two project 1) class library with no inteface just an api 2) web application
from web apps i will be calling the class library 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:
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...
<?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"]
这篇关于从类库中读取 web.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!