c# - App.config 对比Web.config : which has priority?

标签 c# .net configuration app-config web-controls

假设一个项目同时具有 App.config 和 Web.config 文件,其中包含一些相互冲突的信息。是否有任何规则哪个文件具有更高的优先级?或者应用程序必须忽略它们并抛出异常?

最佳答案

来自 .NET 4.5 的 MSDN:

When configuring a service in Visual Studio, use either a Web.config file or an App.config file to specify the settings. The choice of the configuration file name is determined by the hosting environment you choose for the service. If you are using IIS to host your service, use a Web.config file. If you are using any other hosting environment, use an App.config file.

In Visual Studio, the file named App.config is used to create the final configuration file. The final name actually used for the configuration depends on the assembly name. For example, an assembly named "Cohowinery.exe" has a final configuration file name of "Cohowinery.exe.config". However, you only need to modify the App.config file. Changes made to that file are automatically made to the final application configuration file at compile time.

In using an App.config, file the configuration system merges the App.config file with content of the Machine.config file when the application starts and the configuration is applied. This mechanism allows machine-wide settings to be defined in the Machine.config file. The App.config file can be used to override the settings of the Machine.config file; you can also lock in the settings in Machine.config file so that they get used. In the Web.config case, the configuration system merges the Web.config files in all directories leading up to the application directory into the configuration that gets applied. For more information about configuration and the setting priorities, see topics in the System.Configuration namespace.

对于那些使用 MS Azure 的人来说,这里还有一篇很棒的帖子,解释了 ApplicationSettingsappSettings (app.config/web.config) 和 之间的区别配置设置 (.csdef/.cscfg):

http://haishibai.blogspot.com/2012/09/windows-azure-cloud-service.html

关于c# - App.config 对比Web.config : which has priority?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21726272/

相关文章:

ruby-on-rails - Rails 应用程序中的默认 Sidekiq Redis 配置

mysql - 在my.ini/my.cnf中使用环境变量

c# - 我还需要在 c# 中的 redis 缓存中存储什么?

c# - 不同域/项目中的相同界面

.net - 如何在 Spring.net 中注入(inject) Predicate 和 Func

asp.net - 设置 .net Web 应用程序的默认页面

c# - 为什么principalsearcher 代码显着变慢

c# - AutoFixture 可以通过约定注册接口(interface)吗?

c# - LiveLoginResult.Status 未知?

java - 我必须将文件放在哪里才能使用文件附加器 log4j2