ASP.NET web.config:configSource与文件属性


187

在web.configASP.NET应用程序的-file文件中,config的某些部分(例如appSettings和connectionStrings)支持属性file和configSource。

使用file-attribute和configSource-attribute有什么区别?什么时候应该使用哪个属性,并且可以同时使用?

<?xml version="1.0"?>
<configuration>
  <appSettings file="AppSettings.config">
  </appSettings>
  <connectionStrings configSource="ConnectionStrings.config">      
  </connectionStrings>
  <!-- ... -->
</configuration>

Answers:


299

file 属性

configSource 属性

该file属性指定一个包含自定义设置的外部文件,就像在web.config文件的appSettings条目中所做的一样。同时,在configSource属性中指定的外部文件包含您声明configSource的部分的设置。例如,如果您使用configSource页面部分的属性,则外部文件将包含页面部分的设置。

在file属性中指定的外部配置中声明的自定义设置 将与文件中该appSettings 部分中的设置合并web.config。同时,在configSource 不不支持合并,这意味着你必须整节设置移动到外部文件。

http://www.codeproject.com/Messages/1463547/Re-difference-between-configSource-and-file-attrib.aspx


10
另外,“文件”属性允许您在直接目录树之外指定文件,这对于在不同站点之间共享通用设置很重要。不幸的是,“ configsource”属性将您限制为当前树中的文件,因此对于共享设置,您需要在IIS中指定一个虚拟目录。
— 埃德·格雷厄姆

6
我上面的评论并不完全正确,而且我显然错过了编辑它的任意五分钟窗口!您不能在IIS中设置虚拟目录以允许“ configsource”文件位于直接目录树之外。因此,这确实是非常有限的。我通过使用交接点(或NTFS硬链接)解决了问题,但它并不完全漂亮……
— Ed Graham

1
哇!很好的答案...这确实帮助了我:will not cause web application to restart when modifying the specified file。修改自定义部分的外部文件中的任何设置时,我确实需要重新启动应用程序,所以方法是configSource。有趣的是,我正在使用file并且各个部分都在工作。file也可以用于其他部分,appSettings但答案中的解释很清楚。
— Leniel Maccaferri 2014年

4
我想添加到configSource列表中:It must refer to a file in the same directory or in a subdirectory as the configuration file.。以及文件列表:It can reside outside the directory of the configuration file itself.。
— frankhommers 2015年

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.