ASP.NET web.config:configSource 与文件属性 [英] ASP.NET web.config: configSource vs. file attributes
问题描述
在 ASP.NET 应用程序的 web.config
文件中,一些配置部分,如 appSettings
和 connectionStrings
,支持属性 file
和 configSource
.
Within an web.config
-file in an ASP.NET-application some sections of config, like appSettings
and connectionStrings
, supports the attributes file
and configSource
.
使用 file
-attribute 和 configSource
-attribute 有什么区别?什么时候应该使用哪个属性,可以同时使用?
What is the difference between using the file
-attribute and the configSource
-attribute? When should you use which attribute and can you use both?
<?xml version="1.0"?>
<configuration>
<appSettings file="AppSettings.config">
</appSettings>
<connectionStrings configSource="ConnectionStrings.config">
</connectionStrings>
<!-- ... -->
</configuration>
推荐答案
file
属性
- 指定包含自定义应用程序配置设置的外部文件的相对路径
- 特定于
appSettings
部分 - 将合并(并覆盖).config 文件中的设置
- 修改指定文件不会导致web应用重启
- http://msdn.microsoft.com/en-US/library/ms228154(v=vs.100).aspx
- 使用配置.AppSettings.设置.添加 API 将导致所有设置合并回主
.config
上的 配置.保存调用. - 自 .NET 1.1 起
- 如果文件不存在,则不会抛出异常.
- Specifies a relative path to an external file that contains custom application configuration settings
- specific to the
appSettings
section - will merge (and override) settings in the .config file
- will not cause web application to restart when modifying the specified file
- http://msdn.microsoft.com/en-US/library/ms228154(v=vs.100).aspx
- Using the Configuration.AppSettings.Settings.Add API will result in all settings being merged back into the main
.config
on a Configuration.Save call. - since .NET 1.1
- Exception is not thrown if file does not exist.
configSource
属性
- 可以应用于配置文件的大部分部分,而不仅仅是
appSettings
- 将使用外部文件覆盖整个部分,不合并
- 可能导致网络应用程序重新启动
- http://msdn.microsoft.com/en-US/library/system.configuration.sectioninformation.configsource(v=vs.100).aspx
- 使用配置.AppSettings.设置.Add API 将导致所有设置被添加到
configSource
中指定的文件中 配置.保存调用. - 从 .NET 2.0 开始
System.Configuration.ConfigurationErrorsException
如果配置源文件不存在则抛出.
- can apply to most sections of a configuration file, not just
appSettings
- will override the entire section with the external file, no merging
- CAN cause web application to restart
- http://msdn.microsoft.com/en-US/library/system.configuration.sectioninformation.configsource(v=vs.100).aspx
- Using the Configuration.AppSettings.Settings.Add API will result in all settings being added to the file specified in
configSource
on a Configuration.Save call. - since .NET 2.0
System.Configuration.ConfigurationErrorsException
is thrown if config source file does not exist.
file
属性指定一个包含自定义设置的外部文件,就像您在 web.config 文件的 appSettings 条目中所做的那样.同时,在 configSource
属性中指定的外部文件包含您为其声明 configSource 的部分的设置.例如,如果您使用 pages 部分的 configSource
属性,则外部文件将包含 pages 部分的设置.
The file
attribute specifies an external file containing custom settings like you do in the appSettings entry of the web.config file.
Meanwhile, the external file specified in the configSource
attribute contains the settings for the section which you declare the configSource for. For example, if you use the configSource
attribute of the pages section, then the external file will contain the settings for the pages section.
在指定的外部配置中声明的自定义设置file
属性将与 appSettings
中的设置合并web.config
文件中的部分.同时,configSource
不不支持合并,这意味着您必须移动整个部分设置到外部文件中.
The custom settings declared in the external config specified in the
file
attribute will be merged with the settings in theappSettings
section in theweb.config
file. In the meanwhile, theconfigSource
does not support merging, it means that you'll have to move the entire section settings into the external file.
http://www.codeproject.com/Messages/1463547/Re-difference-between-configSource-and-file-attrib.aspx
这篇关于ASP.NET web.config:configSource 与文件属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!