【问题标题】:Separate config file for sections of web.configweb.config 部分的单独配置文件
【发布时间】:2011-05-27 03:01:29
【问题描述】:

是否可以为 web.config 的特定部分设置单独的配置文件?具体来说,我想将 IIS 7 的重写部分从 web.config 移到它自己的配置文件中。

【问题讨论】:

  • 如果您尝试移动 runtime sectionruntime section 很乐意接受 configSource 属性,但它不读取外部文件

标签: asp.net iis-7


【解决方案1】:

您当然可以将重写规则和映射移到单独的文件中:

Storing URL rewrite mappings in a separate file

<system.webServer>
  <rewrite>
    <rewriteMaps configSource="rewritemaps.config" />
    <rules configSource="rewriteRules.config" />
  </rewrite>
</system.webServer>

此外,您可以将相当多的配置部分移动到它们自己的文件中:

&lt;appSettings configSource="appSettings.config" /&gt;[Docs]

&lt;connectionStrings configSource="connectionStrings.config"/&gt;[Docs]

&lt;pages configSource="pages.config"/&gt;[Docs]

有关更多信息,请参阅此页面,该页面将帮助您确定是否可以将配置部分存储在外部:

General Attributes Inherited by Section Elements

【讨论】:

  • 我们遇到了 appSettings 和 MVC 项目的问题。将 appSettings 部分移动到单独的文件后,VS 无法再打开 .cshtml 文件。它显示“操作无法完成”错误。试图弄清楚这一点,我意识到问题是由于 设置造成的。我还尝试使用“file”属性而不是“configSource”来在 web.config 上保留“webpages:version”,但它没有用。有什么想法吗?
  • 运行时部分很乐意接受 configSource 属性,但它不读取外部文件
  • @Luty 有什么解决方案吗? appSettings 使用 configSource 没有问题。问题是另一个问题。
  • @Kiquenet 抱歉回复晚了。我将 appSettings 保留在主配置文件中以便稍后搜索解决方案,但我遇到此问题的项目已停止。
【解决方案2】:

是的,这是可能的。

对于每个配置部分,您可以将configSource 属性设置为您保存配置的位置(文件)。

例如:

<appSettings configSource="appSettings.config" />

appSettings.config中:

<?xml version="1.0" encoding="UTF-8"?>
<appSettings>
    <add key="myKey" value="myValue" />
</appSettings>

【讨论】:

    猜你喜欢
    • 2012-11-29
    • 1970-01-01
    • 2013-02-27
    • 2011-12-19
    • 1970-01-01
    • 2018-09-13
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    相关资源
    最近更新 更多