简体   繁体   中英

Class Library project in ASP.NET reading from web.config and not app.config

I have an ASP.NET solution in which there are two separate projects. One is normal UI and the other one is a class library which is being referred in the former.

In my class library project I am trying to read from App.Config file but it is reading from Web.Config file. I am using:

string url = Convert.ToString(ConfigurationManager.AppSettings["UpdateURL"]);

Initially, this key was written in Web.Config but now I have removed it from there and added this into the App.Config file. However, the compiler is still trying to fetch it from Web.Config. There may be something wrong I am doing. Please tell me what changes I need to make to get this done.

A web application will use web.config .

Keep your app settings inside that. A dll specic config file is not required.

A windows application will use App.config while a web application will use web.config .

If you use your dll in a windows or console application put the setting in app.config.

Dlls will always use the config file of the application they are loaded into. If you want to have a dll specific config file, you will have to load it yourself.

This is normal behavior. Any referenced project will run under the parent's rules.

So if your class library had his own appConfig file, this one is no longer "valid". The Web.Config file has preference.

You need to copy the appSettings section of the class library into the Web.Config file.

You also have the option to "chain" the reading of the settings, it's like, the appSettings section of the web.Config might point to another .config file, but that's just a matter of taste and it's up to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM