问题描述
我必须在 WCF 主机使用的 web.config 文件和 Web 客户端使用的 web.config 文件之间复制一些设置(如连接字符串).
为了不重复,我可以从单独的 xml 文件中读取两个 web.configs 吗?这两个 web.configs 可以完全在不同的解决方案/项目中,所以我想这是不可能的,但想得到其他人的意见.
PS:我知道我可以使用数据库来存储所有配置设置.
是的,任何配置部分都可以外部化" - 这包括之类的东西、
等等.
您的 web.config 中应该有这样的内容:
<appSettings configSource="appSettings.config"/><connectionStrings configSource="connectionStrings.config"/><system.web><pages configSource="pages.config"/><httpHandlers configSource="httphandlers.config"></system.web></配置>外部化的配置将只包含其中的一个小节:
httphandlers.config:
<remove verb="*" path="*.asmx"/><add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/><add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral,PublicKeyToken=31BF3856AD364E35"/><add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate=假"/></httpHandlers>
请注意,您不能将整个 <system.web>
部分外部化,因为这是一个配置 部分组 - 不是配置部分 - 但您可以将大部分内容外部化system.web 中包含的子部分.
I have to duplicate some settings (like connection string) between a web.config file that a WCF host uses and a web.config file that a web client uses.
In the interest of not duplicating, can I have both the web.configs read from a separate xml file? The two web.configs can be entirely in different solutions/projects so I guess this is not possible, but wanted to get other's opinion.
PS: I do understand I can use a database to store all the config settings.
Yes, any configuration section can be "externalized" - this includes things like <appSettings>
, <connectionStrings>
and many more.
You'd have something like this in your web.config:
<configuration>
<appSettings configSource="appSettings.config" />
<connectionStrings configSource="connectionStrings.config" />
<system.web>
<pages configSource="pages.config" />
<httpHandlers configSource="httphandlers.config">
</system.web>
</configuration>
The externalized config's would just contain that one subsection in them:
httphandlers.config:
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
</httpHandlers>
Note you cannot externalize the entire <system.web>
part, since that is a configuration section group - not a configuration section - but you can externalize most of the sub-sections contained in system.web.
这篇关于web.config 可以从外部 xml 文件中读取吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!