简体   繁体   中英

Why do app.config and web.config both exists?

My question might sounds weird, but i can't find any information. How happened, that .net applications have two different types of configuration files: app.config and web.config.

One of my assumptions, that they just came from different development teams. But why not merge them latter?

If somebody knows reason, that would be great, if you can share it

PS I understand, that this question, might be interpreted as offtopic, and will remove it if see such notes in comments

If you do windows applications like console app, windows form, or, windows services, in your project you store your configuration settings in app.config . But when you build that project, the app.config content in stored in a file called yourExeName.exe.config

When you work with asp.net application you work with a web.config. This may differ based on the release configuration (or config transforms). Hence you'd have web.debug.config or web.release.config . But when you finally deploy/publish the application, you'd usually find a web.config .

You still work with an app.config in case you are working with a simple class library project. You may have some settings in there too. So when the dll (ie the output of your class library project) is consumed in a different application you have to make sure those application settings need to be present in your target application, since the app.config stuff don't copy itself into the target assembly's configuration.

Now why are they different? Because I think desktop apps and web apps are inherently different in how they work.

Suggest reading this for getting a better picture. Disclaimer...its from my blog.

Quoting from http://forums.asp.net ( link ):

machine.config is the top level one. It is applied to all sites running on that server. There are some settings which can only be configured in this .config file for security reasons (imagine if you had several different clients hosted on your server). Other settings can be overridden by web.config for example in an individual website. Dont delete this file.

In the root folder of your asp.net website you will find a web.config. This has important information for configuring various elements of your site such as membership providers, any third party libraries, and it extra assemblies you might need. Dont delete this file.

You might also find additional copies of web.config inside sub folders of your site. You can have settings specific to those folders such as setting up security permissions. You will generally find that these additional web.config files are much more lightweight than the main web.config. You could delete these files but depending on what you have in them but doing so might break functionality such as removing the requirement to be logged in to view a certain folder.

App.config is a similar concept to the web.config except it is used in non website projects such as a desktop app or a class library. Again you could delete this file but it might have strange knock on effects with the rest of the code.

All of these .config files are stacked together in memory when the program is run. First the machine.config is read, then if your web site uses class libraries the app.configs are read in and overwrite any conflicting settings currently in memory. Then the main web.config is parsed and any conflicting settings this file contains are used. Then finally if the current request is in a subfolder that contains a web.config that is parsed and ovewrites any conflicting settings as well.

A common example of these conflicting settings is that if you have a database connection string in your class library but need to overwrite it in your website.

There is also an additional file called settings.settings that you might come accross but as far as I can remember its something to do with desktop developement and isnt part of the .config system.

A direct basic hit.

machine.config - Applicable to all sites running on server.

App.config - Desktop Application (Windows Form, WPF etc)

Web.config - Web Application

Please refer ( http://forums.asp.net/t/1671270.aspx?app+config+machine+config+web+config ) for detailed information.

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