我在 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>
我想提取 <customHeaders>
到名为 web.customer.customHeaders.config
的配置文件.为了实现这一点,我创建了 web.customer.customHeaders.config
文件在我的 Web.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>
我还更改了 <customHeaders>
我的部分 Web.config
这样的文件:
<httpProtocol>
<customHeaders configSource="web.customer.customHeaders.config" />
</httpProtocol>
但不幸的是,configSource
属性不被识别。结果,提取的文件无法读取并插入到我的 Web.config
中。文件。
我的问题是:如何从单独文件中的 web.config 中提取一个部分。
如果您对这种管理方式有任何了解,请在下方发表评论。
最佳答案
并非所有部分都允许使用 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
中,您可以找到哪些元素/部分支持此属性;例如。 connectionStrings
和 appSettings
。
关于c# - 如何在单独的文件中从 web.config 中提取一个部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50605452/