reading web.config from class library

后端 未结 5 1911
生来不讨喜
生来不讨喜 2021-01-21 03:30

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

相关标签:
5条回答
  • 2021-01-21 03:32

    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"]

    0 讨论(0)
  • 2021-01-21 03:33

    appSettings is should be like

    <appSettings>
        <add key="employeeDB" value="xxxx" />
    </appSettings>
    
    0 讨论(0)
  • 2021-01-21 03:43

    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

    ConnectingString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

    Hope this will help

    0 讨论(0)
  • 2021-01-21 03:53

    Also, the System.Configuration assembly is not automatically added to a class library.

    1. Right click on the project in the Solution Explorer
    2. Click Add > Reference
    3. Check System.Configuration in the Assemblies > Framework tab
    0 讨论(0)
  • 2021-01-21 03:59

    Your tag is wrong..it should be 'appSettings' not '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>
    
    0 讨论(0)
提交回复
热议问题