从类库中读取web.config [英] reading web.config from class library

查看:129
本文介绍了从类库中读取web.config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i have two project 1) class library with no inteface just an api 2) web application

从网络应用程序中,我将调用类库api

from web apps i will be calling the class library api

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

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???
                }
            }
        }

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

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>

有什么帮助吗?

推荐答案

您的语法不正确,应该是

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>  

,将由ConfigurationManager.ConnectionStrings["employeeDB"]

这篇关于从类库中读取web.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆