web.config 可以从外部 xml 文件中读取吗? [英] Can a web.config read from an external xml file?

查看:28
本文介绍了web.config 可以从外部 xml 文件中读取吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在 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 文件中读取吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆