【问题标题】:How to extract a section from web.config in a seperate file如何在单独的文件中从 web.config 中提取部分
【发布时间】:2018-05-30 13:03:05
【问题描述】:

我在Web.config 中有以下部分:

 <httpProtocol>
  <customHeaders>
    <remove name="X-UA-Compatible" />
    <remove name="X-Frame-Options" />
    <remove name="X-XSS-Protection" />
    <remove name="X-Content-Type-Options" />
    <add name="X-UA-Compatible" value="IE=Edge" />
    <add name="X-Frame-Options" value="DENY" />
    <add name="X-XSS-Protection" value="1; mode=block"></add>
    <add name="X-Content-Type-Options" value="nosniff" />
  </customHeaders>
</httpProtocol>

我想将&lt;customHeaders&gt; 提取到名为web.customer.customHeaders.config 的配置文件中。为了实现这一点,我在我的Web.config 所在的同一位置创建了web.customer.customHeaders.config 文件,并在其中编写了以下XML:

<customHeaders>
    <remove name="X-UA-Compatible" />
    <remove name="X-Frame-Options" />
    <remove name="X-XSS-Protection" />
    <remove name="X-Content-Type-Options" />
    <add name="X-UA-Compatible" value="IE=Edge" />
    <add name="X-Frame-Options" value="DENY" />
    <add name="X-XSS-Protection" value="1; mode=block"></add>
    <add name="X-Content-Type-Options" value="nosniff" />
  </customHeaders>

我还在我的Web.config 文件中更改了&lt;customHeaders&gt; 部分:

 <httpProtocol>
  <customHeaders configSource="web.customer.customHeaders.config" />
</httpProtocol>

但不幸的是,configSource 属性无法识别。结果,提取的文件无法读取并插入到我的Web.config 文件中。

我的问题是:如何从 web.config 中提取一个单独的文件中的部分。

如果您有任何线索如何管理,请在下面发表评论。

【问题讨论】:

  • the configSource attribute is not recognised. IDE 是否以某种方式表明了这一点?或者你是在运行时遇到的?
  • @mjwills:是的。有一条波浪线,当我将鼠标悬停时,提示“不允许使用 'configSource'。”
  • 当你运行它时它会起作用吗?见stackoverflow.com/a/5980776/34092
  • 这可能会有所帮助。 stackoverflow.com/questions/2232059/…
  • configSource 文件是否有一个 xml 元素标头,该标头在此 answer 中指示?

标签: c# asp.net .net vb.net


【解决方案1】:

并非所有部分都允许configSource 属性; customHeaders 就是这样一个。
XSD 架构 Visual Studio 用于验证例如的内容。 web.config 证实了这一点。 您可以在 C:\Program Files (x86)\Microsoft Visual Studio 14.0\Xml\Schemas\1033\DotNetConfig.xsd 中找到此文件(除非您安装在其他位置)。

customHeaders 声明的片段显示没有configSource 属性。

<xs:element name="customHeaders">
  <xs:complexType>
    <xs:choice minOccurs="0" maxOccurs="unbounded">
      <xs:element name="add">
        <!-- ... -->
      </xs:element>
      <xs:element name="remove">
        <!-- ... -->
      </xs:element>
      <xs:element name="clear">
        <!-- ... -->
      </xs:element>
    </xs:choice>
    <xs:anyAttribute />
  </xs:complexType>
</xs:element>

DotNetConfig.xsd 中,您可以找到哪些元素/部分确实支持此属性;例如。 connectionStringsappSettings

【讨论】:

  • 查看了 XSD 文件,现在我正在学习 location 部分/元素是另一个不允许 configSource 属性的部分;该死!。我想将位置元素放在一个单独的文件中,但必须使用更大的 web.config。谢谢 pfx!
猜你喜欢
  • 2011-05-27
  • 1970-01-01
  • 2010-10-11
  • 2019-10-05
  • 2021-01-22
  • 1970-01-01
  • 1970-01-01
  • 2013-02-27
  • 2019-07-12
相关资源
最近更新 更多