Abhinav
Abhinav

Reputation: 65

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.

Upvotes: 0

Views: 6588

Answers (2)

Adrian Salazar
Adrian Salazar

Reputation: 5319

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.

Upvotes: 0

nunespascal
nunespascal

Reputation: 17724

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.

Upvotes: 2

Related Questions